Using Wait Functions
The RWRunnable::wait() functions are used to wait for a runnable to enter any one of a number of execution states. The Threading package has two types of wait() functions:
*One waits an indefinite amount of time for the runnable to enter a targeted execution state.
*One accepts a time-limit value expressed as an unsigned long number of milliseconds.
Both functions take a mask value that specifies one or more execution states that trigger an end to the wait. The execution states of interest should be ORed together to form the mask value.
Because a wait can be satisfied by an entry into any one of several states specified by the mask value, each wait() function also returns the execution state value that triggered the end of the wait.
Using Server Classes. The wait functions are useful for detecting state changes in a runnable that has been passed to another thread for execution. The runnable server classes (discussed in The Server Classes ) accept runnable instances for execution within the server’s thread or threads.
Example. The application code that creates a runnable and passes it to a server class instance might need to know when the runnable began executing. This is accomplished by using one of the wait functions, as shown in Example 14 .
Example 14 – Using a wait function to detect state changes in a runnable
#include <rw/thread/rwRunnable.h>
#include <rw/thread/RWRunnableFunction.h>
#include <rw/thread/RWRunnableSelf.h>
#include <rw/thread/RWServerPool.h>
#include <iostream>
 
using namespace std;
 
void hello() {
RWRunnableSelf self = rwRunnable();
do {
cout << "Hello World!” << endl;
} while(!self.serviceInterrupt());
}
 
void main() {
 
// Create and start a server pool instance
RWServerPool server = RWServerPool::make(3);
server.start();
 
// Create and enqueue a runnable that says hello!
RWRunnable runnable = RWRunnableFunction::make(hello);
server.enqueue(runnable);
 
// Wait for the runnable to start executing
runnable.wait(RW_THR_RUNNING);
 
// OK, that’s enough of that!
if (RW_THR_ACQUIRED == runnable.requestInterrupt())
runnable.releaseInterrupt();
 
// Shutdown the server pool
server.stop();
server.join();
}