c++のsleep関数

6610 ワード


標準ライブラリにこの関数はありません
しかし、いくつかのコンパイルシステムには、いくつかのシステムライブラリには、あなたの環境によって異なります.次のようになります.
linuxには、unsigned int sleep(unsigned int seconds)があり、着信保留時間は、成功して0を返し、成功しなければ残りの秒数を返します.WindowsシステムにはSleep関数(大文字注意)、void Sleep(DWORDdwMilliseconds);保留中のミリ秒数を指定します.
例:
#include #include using namespace std; int main(){Sleep(3000);//3秒Sを一時停止するには大文字return 0;
Use  std::this_thread::sleep_for :
std::chrono::milliseconds timespan(111605); // or whatever

std::this_thread::sleep_for(timespan);

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 _WIN32
    #include 

    void sleep(unsigned milliseconds)
    {
        Sleep(milliseconds);
    }
#else
    #include 

    void sleep(unsigned milliseconds)
    {
        usleep(milliseconds * 1000); // takes microseconds
    }
#endif
But a much simpler pre-C++11 method is to use boost::this_thread::sleep.