windows執行緒yield以及Sleep(0)和SwitchToThread之間的區別

小 樓 一 夜 聽 春 雨發表於2016-01-19

C++的自定義執行緒函式內呼叫了一個自定義的yield()介面。

在windows上是呼叫了SwitchToThread來實現的,linux是pthread_yield實現的。

 

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差不多了。

 

相關文章