TechTip: Running RPG Programs in Multi-Threaded Jobs

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:

  1. Code THREAD(*SERIALIZE) in every module that might be run in a multi-threaded job.
  2. Do not assume that coding THREAD(*SERIALIZE) makes your module thread-safe.
  3. Do not use OPM RPG in a multi-threaded job.
  4. 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 This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: