Changing the Execution State
The callbacks for most state changes are invoked and executed by the thread running inside a runnable, but some state change callbacks are invoked by threads that are manipulating the runnable to bring about a change in execution state.
The Threading package does not specify which thread executes a callback because this can change in future versions of the library. You have to explicitly test to see if the calling thread is associated with the source runnable using the
threadId() and
rwThreadId() functions, as shown in
Example 18.
Example 18 – Testing a calling thread’s identity
void myCallback(const RWRunnable& runnable,
RWExecutionState state)
{
// Are we executing in the runnable’s active thread?
if (runnable.threadId() == rwThreadId())
// This is the runnable’s thread!
else
// This is some other thread!
}
You should take care to avoid making changes in the execution state of a runnable from within a callback function. The Threading package does not prohibit attempts to do so, but the results of such an operation can produce undesirable behavior or result in deadlock.