Running Concurrently in Multiple Threads

When you specify THREAD(*CONCURRENT), two or more threads could be running different procedures in the same module, the same procedure, or even the same statement. When two or more threads are running in the same module or in the same procedure, they each default to having their own instance of the static storage in the module and its procedures. This storage is referred to as thread-local storage. For example, if the two threads happen to be running in the loop below, one thread could be processing the "IF" statement after reading the twentieth record of the file, with variable "count" having a value of 7; the other thread could be processing the "READ" statement after reading the fourth record of the file, with variable "count" having a value of 1.

     read rec ds;
     count = 0;
     dow not %eof(file);
	     if (ds.amtOwing > ds.max.Owing);
           handleAccount (ds);
           count += 1;
        endif;
        read rec ds;
     enddo;

If you have chosen to define a variable with STATIC(*ALLTHREAD), then all threads will use the same instance of that variable.

CAUTION:
RPG does not provide any protection against two threads trying to change an all-thread static variable at the same time, or against one thread trying to change the variable while another thread is checking its value. See All- Thread Static Variables for more information.

If you want to ensure that some code is only used by one thread at a time, you can place the code in a serialized procedure (SERIALIZE keyword on the Procedure-Begin specification). Note that each serialized procedure has its own serialization mechanism; one thread can be running in one serialized procedure while another thread is running in a different serialized procedure in the same module.

Another way to ensure that the code is only used by one thread at a time is to put the code in a procedure to a thread-serialized module.



[ Top of Page | Previous Page | Next Page | Contents | Index ]