boost::this_thread::sleep() 与 nanosleep()?
我最近遇到需要让当前线程休眠一段确切的时间。我知道在 POSIX 平台上执行此操作的两种方法:使用 nanosleep()
或使用 boost::this_thread::sleep()
。
出于好奇,我想知道这两种方法之间有什么区别。精度是否有任何差异?是否有任何理由不使用Boost方法?
nanosleep()
方法:
#include <time.h>
...
struct timespec sleepTime;
struct timespec returnTime;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 1000;
nanosleep(&sleepTime, &returnTime);
Boost 方法:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
...
boost::this_thread::sleep(boost::posix_time::nanoseconds(1000));
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()
.
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 not to use the Boost approach?
nanosleep()
approach:
#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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我能想到的使用 boost 的几个原因:
boost::this_thread::sleep()
是一个boost.thread
boost::this_thread::sleep()
中的中断点由 C++0x 取代
std::this_thread::sleep_until()
中future
为什么不呢——如果您根本不使用线程,或者项目中的其他所有内容都使用 POSIX 调用,那么
nanosleep()
更有意义。至于精度,在我的系统上,boost 和 nanosleep() 都调用相同的系统调用,
hrtimer_nanosleep()
。我想象 boost 作者试图在每个系统上获得尽可能高的精度,对我来说,它恰好与 nanosleep() 提供的一样。The few reasons why use boost that I can think of:
boost::this_thread::sleep()
is aninterruption point in boost.thread
boost::this_thread::sleep()
can bedrop-in replaced by C++0x's
std::this_thread::sleep_until()
infuture
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.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 whatnanosleep()
provides.怎么样,因为你的nanonsleep例子是错误的。
诚然,如果您只睡 1 微秒,那么早起应该不是问题,但在一般情况下,这是完成任务的唯一方法。
为了给 boost 锦上添花,
boost::this_thread::sleep()
是使用nanosleep()
实现的。他们只是为你处理了所有疯狂的极端情况。How about because your nanonsleep example is wrong.
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.
And just to ice the cake in boost's favor,
boost::this_thread::sleep()
is implemented usingnanosleep()
. They just took care of all the insane corner cases for you.我认为这是显而易见的,但我能想到的唯一原因是您需要 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 变体的主要原因是平台独立性。例如,如果您需要为 posix 和 Windows 平台编译应用程序,则平台睡眠是不够的。
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.