Windows Programming-thread Switching

Source: Internet
Author: User


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.