Xbasic

YIELD CONTROL

Syntax

YIELD CONTROL

Description

Allow a foreground thread to run via a background thread.

Discussion

A background thread uses the YIELD CONTROL command to allow a foreground thread to run. YIELD CONTROL causes the current thread to yield. If there are any other threads waiting to use the CPU, they will be able to do so.

Example code - a background thread counts up. 

dim global running as L
dim global counter as N

running = .t.
thread_create("counter",<<%code%
#while running
# yield control
# counter = counter + 1
#end while
#%code%)

? counter
= 178

? counter
= 301

? thread_enum()
= Main
counter

Without the yield control, the background thread would not relinquish control to the thread_enum() command (and Alpha Anywhere would hang). If your background thread uses THREAD_WAIT() it does not need a yield control since THREAD_WAIT() yeilds an waits for another thread to call THREAD_EVENT(). If your background thread uses socket API calls for streamed file API calls, it doesn't need a YIELD CONTROL because the socket and streamed file calls are preemptive (and return control to the main thread).

Limitations

Desktop Applications Only

See Also