C++的自定義線程函數內調用了一個自定義的yield()接口。linux
在windows上是調用了SwitchToThread來實現的,linux是pthread_yield實現的。windows
Sleep(0):時間片只能讓給優先級相同或更高的線程;
SwitchToThread():只要有可調度線程,即使優先級較低,也會讓其調度。函數
下面是MSDN上對Sleep函數的描述:
The time interval for which execution is to be suspended, in milliseconds.
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
Windows XP/2000: A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. This behavior changed starting with Windows Server 2003.
能夠看到,從2003 server開始,Sleep(0)變成了調度全部可調度線程,跟SwitchToThread差很少了。ui