Product SiteDocumentation Site

Chapter 12. Concurrency

12.1. Early Reply
12.2. Message Objects
12.3. Default Concurrency
12.3.1. Sending Messages within an Activity
12.4. Using Additional Concurrency Mechanisms
12.4.1. SETUNGUARDED Method and UNGUARDED Option
12.4.2. GUARD ON and GUARD OFF
12.4.3. Guarded Methods
12.4.4. Additional Examples
Conceptually, each Rexx object is like a small computer with its own processor to run its methods, its memory for object and method variables, and its communication links to other objects for sending and receiving messages. This is object-based concurrency. It lets more than one method run at the same time. Any number of objects can be active (running) at the same time, exchanging messages to communicate with, and synchronize, each other.

12.1. Early Reply

Early reply provides concurrent processing. A running method returns control, and possibly a result, to the point from which it was called; meanwhile it continues running. The following figure illustrates this concept.
Early Reply

Figure 12.1. Early Reply


Method A includes a call to Method B. Method B contains a REPLY instruction. This returns control and a result to method A, which continues processing with the line after the call to Method B. Meanwhile, Method B also continues running.
The chains of execution represented by method A and method B are called activities. An activity is a thread of execution that can run methods concurrently with methods on other activities.
An activity contains a stack of invocations that represent the Rexx programs running on the activity. An invocation can be a main program invocation, an internal function or subroutine call, an external function or subroutine call, an INTERPRET instruction, or a message invocation. An invocation is activated when an executable unit is invoked and removed (popped) when execution completes. In Figure 12.1, “Early Reply”, the programs begins with a single activity. The activity contains a single invocation, method A. When method A invokes method B, a second invocation is added to the activity.
When method B issues a REPLY, a new activity is created (activity 2). Method B's invocation is removed from activity 1, and pushed on to activity 2. Because activities can execute concurrently, both method A and method B continue processing. The following figures illustrate this concept.
Before REPLY

Figure 12.2. Before REPLY


After REPLY

Figure 12.3. After REPLY


Here is an example of using early reply to run methods concurrently.

Example 12.1. REPLY instruction

/* Example of early reply */

object1 = .example~new
object2 = .example~new

say object1~repeat(10, "Object 1 running")
say object2~repeat(10, "Object 2 running")
say "Main ended."
exit

::class example
::method repeat
use arg reps,msg
reply "Repeating" msg"," reps "times."
do reps
  say msg
end