The system providesSwitchToThreadTo make another schedulable thread (if any ):
BOOL SwitchToThread ();
When calling this function, the system needs to check whether there is a thread that urgently needs CPU time. If no thread urgently needs CPU time, SwitchToThread will return immediately. If there is a thread that urgently requires CPU time, SwitchToThread schedules the thread (the priority of the thread may be lower than the thread that calls SwitchToThread ). This thread that urgently needs CPU time can run for a period of time, and then the system scheduling program runs as usual.
This function allows a thread that requires a resource to force another thread with a lower priority to discard the resource (to seize the resource ). If no other thread can run the SwitchToThread function, fals e is returned. Otherwise, a non-0 value is returned.
Similarities and differences between SwitchToThread and Sleep
The call to the SwitchToThread function is similar to the call to Sleep and a 0 Ms timeout is passed to it.Differences:It is a thread running with a lower priority of SwitchToThread. Even if the low-priority thread urgently needs the c p u time, Sleep can immediately reschedule the calling thread.
(FangSH Note: Some people say this on the Internet: the difference is that SwitchToThread allows low-priority threads to be executed, and Sleep will immediately reschedule the main thread, even if the low-priority thread will be in the hunger state .)
FangSH2010-12-28