Threads Module User's Guide : PART II Concurrency Packages : Chapter 3 The Threading Package : The Runnable Object Classes : Yielding Execution
Yielding Execution
A thread can use a yield operation to free processing resources for the execution of other threads. The yield operation can be used in continuous or compute-bound operations to give other threads a chance to make progress.
You can use two different functions to yield execution:
void RWRunnableSelf::yield(void)
The RWRunnableSelf::yield() function can be called by a thread running inside of a runnable object. This function sets the runnable execution state to RW_THR_YIELDING[16] before yielding execution. The execution state is reset to RW_THR_RUNNING[17] once the thread begins executing again.
void rwYield(void)
The global rwYield() function can be called by any thread, regardless of whether or not it originated or is executing within a runnable object. This function is identical to the yield() function except that it causes no changes in the execution state of a runnable.
Executing Under a Preemptive Scheduling Policy
When running under a preemptive thread scheduling policy, a thread does not give up its processor unless blocked by a synchronization mechanism or preempted by a higher priority thread. If a thread executing under this scheduling policy does not block, no other threads of the same or lesser priority get a chance to execute on the processor.
Yield can be used in this situation to give other threads of the same priority a chance to execute. A thread that yields is usually moved to the end of its current priority queue and does not get a chance to execute again until all other threads of the same priority have had a chance.
A yield operation does not force a thread to give up the processor if no other threads of the same priority are currently eligible for execution. A yield in this situation has no affect on the scheduling of the thread.
Yielding to Lower Priority Threads
To yield execution to threads of lower priority, it might be necessary to use the sleep() or rwSleep() functions.