C ++的跨平台睡眠功能 [英] Cross platform Sleep function for C++
本文介绍了C ++的跨平台睡眠功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有可能用宏做跨平台的睡眠代码吗?
例如
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);
...
推荐答案
你所做的是在你自己的函数中包含不同的系统sleeps调用以及如下的include语句:
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
}
然后你的代码调用 mySleep
进入休眠状态,而不是直接进行系统调用。
Then your code calls mySleep
to sleep rather than making direct system calls.
这篇关于C ++的跨平台睡眠功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文