C++ boost::this_thread::sleep() 与 nanosleep() 对比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3072912/
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
boost::this_thread::sleep() vs. nanosleep()?
提问by Justin Ardini
I recently came across the need to sleep the current thread for an exact period of time. I know of two methods of doing so on a POSIX platform: using nanosleep()or using boost::this_thread::sleep().
我最近遇到需要让当前线程休眠一段确切的时间。我知道在 POSIX 平台上这样做的两种方法:使用nanosleep()或使用boost::this_thread::sleep().
Out of curiosity more than anything else, I was wondering what the differences are between the two approaches. Is there any difference in precision, and is there any reason notto use the Boost approach?
出于好奇,我想知道这两种方法之间有什么区别。精度上有什么不同,有什么理由不使用Boost方法吗?
nanosleep()approach:
nanosleep()方法:
#include <time.h>
...
struct timespec sleepTime;
struct timespec returnTime;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 1000;
nanosleep(&sleepTime, &returnTime);
Boost approach:
提升方法:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
...
boost::this_thread::sleep(boost::posix_time::nanoseconds(1000));
回答by Cubbi
The few reasons why use boost that I can think of:
我能想到的使用 boost 的几个原因:
boost::this_thread::sleep()is an interruption point in boost.threadboost::this_thread::sleep()can be drop-in replaced by C++0x'sstd::this_thread::sleep_until()in future
boost::this_thread::sleep()是 boost.thread 中的一个中断点boost::this_thread::sleep()std::this_thread::sleep_until()将来可以直接替换为 C++0x
For why not -- if you're not using threads at all, or of everything else in your project uses POSIX calls, then nanosleep()makes more sense.
为什么不 - 如果您根本不使用线程,或者您的项目中的其他所有内容都使用 POSIX 调用,那么nanosleep()更有意义。
As for precision, on my system both boost and nanosleep() call the same system call, hrtimer_nanosleep(). I imagine boost authors try to get the highest precision possible on each system and for me it happens to be the same thing as what nanosleep()provides.
至于精度,在我的系统上 boost 和 nanosleep() 都调用相同的系统调用hrtimer_nanosleep(). 我想象 boost 作者试图在每个系统上获得尽可能高的精度,而对我来说,这恰好与所nanosleep()提供的相同。
回答by deft_code
How about because your nanonsleep example is wrong.
怎么样,因为您的 nanonsleep 示例是错误的。
#include <time.h>
...
struct timespec sleepTime;
struct timespec time_left_to_sleep;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 1000;
while( (sleepTime.tv_sec + sleepTime.tv_nsec) > 0 )
{
nanosleep(&sleepTime, &time_left_to_sleep);
sleepTime.tv_sec = time_left_to_sleep.tv_sec;
sleepTime.tv_nsec = time_left_to_sleep.tv_nsec;
}
Admittedly if you're only sleeping for 1 microsecond waking up too early shouldn't be an issue, but in the general case this is the only way to get it done.
诚然,如果你只睡了 1 微秒,那么过早醒来应该不是问题,但在一般情况下,这是完成它的唯一方法。
And just to ice the cake in boost's favor, boost::this_thread::sleep()is implemented using nanosleep(). They just took care of all the insane corner cases for you.
并且只是为了让 boost 更受欢迎,boost::this_thread::sleep()使用nanosleep(). 他们只是为您处理了所有疯狂的角落案例。
回答by Billy ONeal
is there any reason not to use the Boost approach
有什么理由不使用 Boost 方法
I suppose this is kind of obvious, but the only reason I can think of is that you'd require boost to compile your project.
我想这很明显,但我能想到的唯一原因是你需要 boost 来编译你的项目。
回答by MM.
For me the main reason for using the boost variant is platform independence. If you are required to compile your application for both posix and Windows platforms, for example, the platform sleep is not sufficient.
对我来说,使用 boost 变体的主要原因是平台独立性。例如,如果您需要为 posix 和 Windows 平台编译应用程序,则平台睡眠是不够的。

