C++中的睡眠函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:47:39  来源:igfitidea点击:

Sleep function in C++

c++

提问by GManNickG

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

是否有类似的函数Sleep(time);将程序暂停 X 毫秒,但在 C++ 中?

Which header should I add and what is the function's signature?

我应该添加哪个标头以及函数的签名是什么?

回答by GManNickG

Use std::this_thread::sleep_for:

使用std::this_thread::sleep_for

std::chrono::milliseconds timespan(111605); // or whatever

std::this_thread::sleep_for(timespan);

There is also the complementary std::this_thread::sleep_until.

还有互补的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 sleepfunction for Windows or Unix:

在 C++11 之前,C++ 没有线程概念和睡眠功能,因此您的解决方案必然依赖于平台。这是sleep为 Windows 或 Unix定义函数的片段:

#ifdef _WIN32
    #include <windows.h>

    void sleep(unsigned milliseconds)
    {
        Sleep(milliseconds);
    }
#else
    #include <unistd.h>

    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.

但是一个更简单的 C++11 之前的方法是使用boost::this_thread::sleep.

回答by Jostein Topland

You'll need at least C++11.

你至少需要 C++11。

#include <thread>
#include <chrono>

...

std::this_thread::sleep_for(std::chrono::milliseconds(200));

回答by dicroce

On Unix, include #include <unistd.h>.

在 Unix 上,包括#include <unistd.h>.

The call you're interested in is usleep(). Which takes microseconds, so you should multiply your millisecond value by 1000 and pass the result to usleep().

您感兴趣的电话是usleep()。这需要微秒,因此您应该将毫秒值乘以 1000 并将结果传递给usleep().

回答by Dani

For Windows:

对于 Windows:

#include "windows.h" 
Sleep(10);

For Unix:

对于 Unix:

#include <unistd.h>
usleep(10)

回答by alexkr

Prior to C++11, there was no portable way to do this.

在 C++11 之前,没有可移植的方法来做到这一点。

A portable way is to use Boost or Acelibrary. There is ACE_OS::sleep();in ACE.

一种可移植的方式是使用 Boost 或Ace库。有ACE_OS::sleep();在ACE。

回答by Prasang Singhal

Just use it...

就用它...

Firstly include the unistd.hheader file, #include<unistd.h>, and use this function for pausing your program execution for desired number of seconds:

首先包含unistd.h头文件 ,#include<unistd.h>并使用此函数将程序执行暂停所需的秒数:

sleep(x);

xcan take any value in seconds.

x可以在几秒钟内取任何值。

If you want to pause the program for 5 seconds it is like this:

如果你想暂停程序 5 秒钟,它是这样的:

sleep(5);

It is correct and I use it frequently.

这是正确的,我经常使用它。

It is valid for C and C++.

它对 C 和 C++ 有效。

回答by Merav Kochavi

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

我为 C++ 11 找到的最简单的方法是:

Your includes:

您的包括:

#include <chrono>
#include <thread>

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

您的代码(这是 sleep 1000 毫秒的示例):

std::chrono::duration<int, std::milli> timespan(1000);
std::this_thread::sleep_for(timespan);

The duration could be configured to any of the following:

持续时间可以配置为以下任何一项:

std::chrono::nanoseconds   duration</*signed integer type of at least 64 bits*/, std::nano>
std::chrono::microseconds  duration</*signed integer type of at least 55 bits*/, std::micro>
std::chrono::milliseconds  duration</*signed integer type of at least 45 bits*/, std::milli>
std::chrono::seconds       duration</*signed integer type of at least 35 bits*/, std::ratio<1>>  
std::chrono::minutes       duration</*signed integer type of at least 29 bits*/, std::ratio<60>>
std::chrono::hours         duration</*signed integer type of at least 23 bits*/, std::ratio<3600>>