If your programs (or service programs) might be run in multi-threaded jobs, then they must be thread-safe. Among other things, thread-safety requires that if you have any storage that can be accessed by more than one thread, you must ensure that threads do not interfere with each other when they use the storage.
The static storage used by a program can be accessed by multiple threads, and RPG programs are heavily dependent on static storage. If you have an ILE RPG subprocedure that uses only local automatic variables (no global variables), that subprocedure is still dependent on some internal static storage added to your program to handle the statements in your procedure.
If you allow multiple threads to have simultaneous access to the static storage in an ILE RPG module, you may see some very strange, unpredictable, and usually irreproducible results.
To protect the static storage in an ILE RPG module from access by more than one thread at once, you must code THREAD(*SERIALIZE) on the H spec. This causes the RPG compiler to generate code to ensure that only one thread can be running in any procedure in the module at any one time. If thread A is running procedure PROC_ONE and thread B wants to call PROC_TWO in the same module, thread B must wait until thread A has finished running PROC_ONE before thread B's call can complete. This ensures that only one thread can access the static storage at once.
However, while using THREAD(*SERIALIZE) is required for thread-safety, it does not ensure thread-safety. Protection of the module's static storage is only one aspect of thread-safety, and many other aspects of thread-safety cannot be handled by simply serializing access to your ILE RPG modules.
Here are some rules of thumb:
- Code THREAD(*SERIALIZE) in every module that might be run in a multi-threaded job.
- Do not assume that coding THREAD(*SERIALIZE) makes your module thread-safe.
- Do not use OPM RPG in a multi-threaded job.
- Understand everything that "thread-safety" entails before putting a multi-threaded application into production.
For more information on using threads, read the following topics in the
V5R4 Information Center:
- Navigate to Programming -> Languages -> RPG -> ILE RPG -> ILE RPG Programmer's Guide. Start with the "Multithreaded Applications" topic in Chapter 2. Follow the link to "Multithreading Considerations" and read to the end of the chapter.
- Navigate to Programming -> Multithreaded Applications. Read all the topics carefully.
Barbara Morris joined IBM in 1989 after graduating from the University of Alberta with a degree in computing science. Within IBM, she has always worked on the RPG Compiler team. You can contact Barbara at
LATEST COMMENTS
MC Press Online