Threads Module User's Guide : PART II Concurrency Packages : Chapter 3 The Threading Package : The Runnable Object Classes : Joining a Runnable
Joining a Runnable
The RWRunnable::join() function waits for a runnable to complete execution. You can join with both synchronous and threaded runnables. It is not necessary for the thread that starts a synchronous runnable to join with that runnable because the runnable has completed execution when it returns from start().
The join operation is implemented by the following functions:
void RWRunnable::join(void)
RWWaitStatus RWRunnable::join(unsigned long milliseconds)
A runnable can be joined by any number of threads and any number of times by the same thread.
Figure 11 shows the timing-sequence and state changes associated with the join operation.
Figure 11 – Join operation — interaction and timing
Types of Join Functions
The Threading package has two types of join functions:
One waits indefinitely for the runnable to complete execution.
One accepts a time-limit for the wait.
Several functions in the Threading package accept a timeout value. This value is meant to specify the amount of “wall-clock” time, in milliseconds, that a function waits before timing out.
NOTE >> In some situations, functions that accept a timeout value actually measure duration by the amount of CPU time used, not by the elapsed wall-clock time.
This may result in longer timeout periods than are expected or intended. In these situations, the amount of delay is directly proportional to the percentage of available CPU time granted to the process.
The code segment in Example 6 illustrates both types of join functions.
Example 6 – Joining runnables in two ways
// Create a threaded runnable
RWThreadFunction myThread = ...
 
// Start the runnable
myThread.start();
 
// Wait 1000 msecs (1 second) for the runnable’s thread to exit.
if (RW_THR_TIMEOUT == myThread.join(1000)) {
// The runnable did not finish within one second!
// Now wait as long as it takes...
myThread.join();
}
Joining Unstarted or Inactive Runnables
If you join a runnable that has never been started, the join() function waits for the runnable to start and complete. If you join a runnable that is currently inactive, but has already been started previously, the join() function returns immediately. To wait for the next completion on a runnable that has already completed one or more executions, you need to delay until after the runnable has been restarted, as indicated by the RW_THR_STARTING execution state or by a completion state of RW_THR_PENDING.