IBM AS/400 Frozen Dessert Maker User Manual


 
Multithreading Considerations
| THREAD(*SERIALIZE) will protect most of your variables and all your internal
| control structures from being accessed improperly by multiple threads. The thread
| safe module will be locked when a procedure in the module is entered and
| unlocked when no procedure in the module is still running. This serialized access,
| ensures that only one thread is active in any one module, within an activation
| group, at any one time. However, it is still up to the programmer to handle thread
| safety for storage that is shared across modules. This is done by adding logic in
| the application to synchronize access to the storage. For example, shared files,
| exported and imported storage, and storage accessed by the address of a param-
| eter may be shared across modules from multiple threads. To synchronize access
| to this type of storage, you can do one or both of the following:
| Structure the application such that the shared resources are not accessed
| simultaneously from multiple threads.
| If you are going to access resources simultaneously from separate threads,
| synchronize access using facilities such as semaphores or mutexes. For more
| information, refer to the Multithreaded Applications document under the Pro-
| gramming topic at the following URL:
|
http://www.as400.ibm.com/infocenter/
| How to Share Data Across More Than One Module
| Serializing access to modules using the THREAD(*SERIALIZE) control specification
| keyword ensures sequential access to global data within each module, but it does
| not ensure sequential access to shared data across modules. It is up to the pro-
| grammer to ensure that only one thread can access shared data at one time.
| Two or more modules can access the same data if:
| EXPORT/IMPORT keywords are used on the definition specifications
| Files are shared across modules
| Data is based on a pointer where the pointer is available to more than one
| module
| For example, procedure A in module A passes a pointer to procedure B in module
| B, and procedure B saves the pointer in a static variable. Now both modules have
| access to the based storage at the same time as the thread running in module A is
| accessing the storage. Once procedure B returns, another thread could call a pro-
| cedure in module B and access the based storage. Serialization of access to static
| storage within modules A and B would not prevent simultaneous access of the
| same storage in each module. The following is an example of two modules that can
| access the same data.
158 ILE RPG for AS/400 Programmer's Guide