boost::this_thread::sleep() 与 nanosleep()?

发布于 2024-09-06 20:35:34 字数 649 浏览 4 评论 0原文

我最近遇到需要让当前线程休眠一段确切的时间。我知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国粹 2024-09-13 20:35:34

我能想到的使用 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 an
    interruption point in boost.thread
  • boost::this_thread::sleep() can be
    drop-in replaced by C++0x's
    std::this_thread::sleep_until() in
    future

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 what nanosleep() provides.

满身野味 2024-09-13 20:35:34

怎么样,因为你的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;
}

诚然,如果您只睡 1 微秒,那么早起应该不是问题,但在一般情况下,这是完成任务的唯一方法。

为了给 boost 锦上添花,boost::this_thread::sleep() 是使用 nanosleep() 实现的。他们只是为你处理了所有疯狂的极端情况。

How about because your nanonsleep example is wrong.

#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.

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.

一笑百媚生 2024-09-13 20:35:34

有什么理由不使用Boost方法

我认为这是显而易见的,但我能想到的唯一原因是您需要 boost 来编译您的项目。

is there any reason not to use the Boost approach

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.

遗弃M 2024-09-13 20:35:34

对我来说,使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文