IBM AS/400 Frozen Dessert Maker User Manual


 
Multithreading Considerations
| *-----------------------------------------------------------------------------------
| * .---------------.
| * | |
| * | some storage |<---------------- pointer to shared storage
| * | | (called MyPtr in module A)
| * '---------------' (saved as a static variable in module B)
| * Module A
| * Global variables in Module A
| D MyPtr S *
| D SomeStorage S 10A based(MyPtr)
| C eval SomeStorage = 'Init value'
| C callp ProcB(MyPtr) 2
| C eval SomeStorage = *BLANKS 3
| *-----------------------------------------------------------------------------------
| * Module B
| * Global variables in Module B
| D SavedPtr S *
| D SomeStorage S 10A based(SavedPtr)
| * ProcB in module B
| P ProcB B export
| D ProcB PI
| D PtrParm *
| C eval SavedPtr = PtrParm 6
| C return 7
| P E
| * ProcB2 in module B
| P ProcB2 B export
| D ProcB2 PI
| D PtrParm *
| C if SomeStorage = 'Init value' 8
| C ....
| C return
| P E
|
Figure 73. Example of Sharing Data in a Multithreaded Environment
| When ProcA calls ProcB (line 2), no other thread can access the storage pointed
| to by MyPtr, since both module A and and module B are being used by one thread.
| ProcB saves the pointer in module B's static storage (line 6) and returns (line
| 7). Now, no thread is active in module B, so another thread is free to call module
| B. If another thread calls ProcB2, it is possible that the first thread could process
| line 3 before, at the same time, or after the second thread processes line 8.
| The order of these events is not defined; the code used to test if SomeStorage =
| 'Init value' could succeed one time and fail the next time.
| You can synchronize access to the shared data, by using logic in the program or by
| using synchronization techniques provided by C or by platform functions. For more
| details, refer to the Multithreaded Applications document under the Programming
| topic at the following URL:
|
http://www.as400.ibm.com/infocenter/
| How to Avoid Deadlock Between Modules
| In some situations, it may be necessary for you to control the synchronization of
| modules using facilities other than the THREAD(*SERIALIZE) control specification
| keyword. For example, consider the situation where two procedures are being
| called at the same time: PROC1 and PROC3. Even though there is no actual recur-
| sive calling; if PROC1 calls PROC4, it will wait for MOD2 to unlock; and if PROC3
| calls PROC2, it will wait for MOD1 to unlock. The procedures will not be able to
Chapter 10. Calling Programs and Procedures 159