Sleep function in C++

后端 未结 7 1698
情书的邮戳
情书的邮戳 2020-11-27 03:27

Is there a function like Sleep(time); that pauses the program for X milliseconds, but in C++?

Which header should I add and what is the function\'s sign

7条回答
  •  甜味超标
    2020-11-27 03:37

    The simplest way I found for C++ 11 was this:

    Your includes:

    #include 
    #include 
    

    Your code (this is an example for sleep 1000 millisecond):

    std::chrono::duration timespan(1000);
    std::this_thread::sleep_for(timespan);
    

    The duration could be configured to any of the following:

    std::chrono::nanoseconds   duration
    std::chrono::microseconds  duration
    std::chrono::milliseconds  duration
    std::chrono::seconds       duration>  
    std::chrono::minutes       duration>
    std::chrono::hours         duration>
    

提交回复
热议问题