C++ 的跨平台睡眠函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10918206/
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
Cross platform Sleep function for C++
提问by ElSajko
Is it possible with macros make cross platform Sleep code? For example
是否可以使用宏制作跨平台睡眠代码?例如
#ifdef LINUX
#include <header_for_linux_sleep_function.h>
#endif
#ifdef WINDOWS
#include <header_for_windows_sleep_function.h>
#endif
...
Sleep(miliseconds);
...
回答by Michael Dorst
Yup. But this only works in C++11and later.
#include <chrono>
#include <thread>
...
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
where ms
is the amount of time you want to sleep in milliseconds.
ms
你想以毫秒为单位的睡眠时间在哪里。
You can also replace milliseconds
with nanoseconds
, microseconds
, seconds
, minutes
, or hours
. (These are specializations of the type std::chrono::duration.)
您也可以替换milliseconds
使用nanoseconds
,microseconds
,seconds
,minutes
,或hours
。(这些是std::chrono::duration类型的特化。)
Update:In C++14, if you're sleeping for a set amount of time, for instance 100 milliseconds, std::chrono::milliseconds(100)
can be written as 100ms
. This is due to user defined literals, which were introduced in C++11. In C++14the chrono
library has been extended to include the following user defined literals:
更新:在C++14 中,如果您睡眠了一定时间,例如 100 毫秒,std::chrono::milliseconds(100)
则可以写为100ms
. 这是由于在C++11中引入的用户定义文字。在C++14 中,库已扩展为包括以下用户定义的文字:chrono
std::literals::chrono_literals::operator""h
std::literals::chrono_literals::operator""min
std::literals::chrono_literals::operator""s
std::literals::chrono_literals::operator""ms
std::literals::chrono_literals::operator""us
std::literals::chrono_literals::operator""ns
std::literals::chrono_literals::operator""h
std::literals::chrono_literals::operator""min
std::literals::chrono_literals::operator""s
std::literals::chrono_literals::operator""ms
std::literals::chrono_literals::operator""us
std::literals::chrono_literals::operator""ns
Effectively this means that you can write something like this.
实际上,这意味着您可以编写类似的内容。
#include <chrono>
#include <thread>
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(100ms);
Note that, while using namespace std::literals::chrono_literals
provides the least amount of namespace pollution, these operators are also available when using namespace std::literals
, or using namespace std::chrono
.
请注意,虽然using namespace std::literals::chrono_literals
提供最少的命名空间污染,但这些运算符在using namespace std::literals
、 或时也可用using namespace std::chrono
。
回答by shf301
Yes there is. What you do is wrap the different system sleeps calls in your own function as well as the include statements like below:
就在这里。您所做的是将不同的系统睡眠调用包装在您自己的函数中以及如下所示的包含语句中:
#ifdef LINUX
#include <unistd.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif
void mySleep(int sleepMs)
{
#ifdef LINUX
usleep(sleepMs * 1000); // usleep takes sleep time in us (1 millionth of a second)
#endif
#ifdef WINDOWS
Sleep(sleepMs);
#endif
}
Then your code calls mySleep
to sleep rather than making direct system calls.
然后您的代码调用mySleep
睡眠而不是直接进行系统调用。
回答by D?enan
shf301 had a good idea, but this way is better:
shf301 有一个好主意,但这种方式更好:
#ifdef _WINDOWS
#include <windows.h>
#else
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
#endif
Then use like this:
然后像这样使用:
Sleep(how_many_milliseconds);
回答by Mike C
回答by Joshua
The stock solution is the select() call (requires Winsock). This particular call has exactly the same behavior on Linux and Windows.
常用的解决方案是 select() 调用(需要 Winsock)。此特定调用在 Linux 和 Windows 上具有完全相同的行为。
long value; /* time in microseconds */
struct timeval tv;
tv.tv_sec = value / 1000000;
tv.tv_usec = value % 1000000;
select(0, NULL, NULL, NULL, &tf);
回答by The Simba
In linux remember that usleep has a limit. You can't 'sleep' more than 1000 seconds.
在 linux 中记住 usleep 有一个限制。你不能“睡觉”超过 1000 秒。
I would write like this
我会这样写
struct timespec req={0},rem={0};
req.tv_sec=(milisec/1000);
req.tv_nsec=(milisec - req.tv_sec*1000)*1000000;
nanosleep(&req,&rem);