提高:: this_thread ::睡眠()对了nanosleep()? [英] boost::this_thread::sleep() vs. nanosleep()?

查看:960
本文介绍了提高:: this_thread ::睡眠()对了nanosleep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了需要睡眠当前线程的时间的确切期限来了。我知道这样做对POSIX平台的两种方法:使用了nanosleep()或使用的boost :: this_thread ::睡眠()

出于好奇,比什么都重要,我想知道的区别是这两种方法之间的东西。是否有precision任何区别,而且是没有任何理由的的使用升压的方法呢?

了nanosleep()办法:

 的#include<&time.h中GT;
...
结构的timespec睡眠时间;
结构的timespec返回时间;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 1000;
了nanosleep(安培;睡眠时间,和放大器;返回时间);

升压方法:

 的#include<升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/线程/ thread.hpp>
...
提高:: this_thread ::睡眠(升压::了posix_time ::纳秒(1000));


解决方案

的几个原因使用升压,我能想到的:


  • 的boost :: this_thread ::睡眠()是一个
    在boost.thread断点

  • 的boost :: this_thread ::睡眠()
    插入式用C ++ 0x中的替换
    的std :: this_thread :: sleep_until()
    未来

有关为什么不 - 如果你不使用线程所有,或在您的项目一切的使用POSIX调用,那么了nanosleep()更有意义。

至于precision,我的系统都提升和了nanosleep()调用同一个系统调用上, hrtimer_nanosleep()。我想提高作者试图以获得最高precision可能在每个系统上,对我来说正好是同样的事情是什么了nanosleep()提供。

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));

解决方案

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.

这篇关于提高:: this_thread ::睡眠()对了nanosleep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆