Sleep(0) 的意義是放棄當前線程執行的時間片,把自身放到等待隊列之中。這時其它的線程就會得到時間片進行程序的程序。Sleep(0)能夠降低當前線程的執行速 度,比如:現在系統中有100個線程(先假設是線程吧)在執行不同的任務,並且它們執行的優先級都是一樣的,並且它們每一次分配的時間片的長度都是一樣 的。那么現在當前線程中有一個Sleep(0),那么對於當前線程獲得時間片的等待時間延長了1倍,也就是說相當於 200 個時間片之后再得到時間片執行任務。
標准庫中無該函數
但在某些編譯系統中有,在有些系統庫中有,要根據你那邊的環境而定。
如:
linux中有,unsigned int sleep(unsigned int seconds),傳入掛起時間,成功返回0,不成功則返回余下的秒數。
windows系統中有Sleep函數(注意大寫),void Sleep(DWORD dwMilliseconds); 提供掛起的毫秒數。
Sleep就是把自己掛起,結束當前時間片
例如:
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
Sleep(3000);//暫停3秒 S要大寫
return 0;
}
Use std::this_thread::sleep_for
:
::::(111605// or whatever::::();
There is also the complimentary std::this_thread::sleep_until
.
Prior to C++11, C++ had no thread concept and no sleep capability, so your solution was necessarily platform dependent. Here's a snippet that defines a sleep
function for Windows or Unix:
#ifdef#include<windows.h>void(unsigned){Sleepmilliseconds}#else#include<unistd.h>void(unsigned){(*1000// takes microseconds}#endif
But a much simpler pre-C++11 method is to use boost::this_thread::sleep