boost::this_thread::sleep() vs. nanosleep()?

28,747

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
28,747
Justin Ardini
Author by

Justin Ardini

Student and Developer at Brown University. Interests: Game programming, mobile development, computer graphics, web app development, programming language design. Experienced in C++, Python, Java, and JavaScript.

Updated on July 09, 2022

Comments

  • Justin Ardini
    Justin Ardini about 2 years

    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));
    
  • Billy ONeal
    Billy ONeal about 14 years
    +1 -- it should be noted that timing based upon thread sleeps -- even nanosleep -- is unreliable at best.
  • Justin Ardini
    Justin Ardini about 14 years
    I suppose I should have said the project already uses other boost libraries. Nonetheless, a valid answer to the general question.
  • Justin Ardini
    Justin Ardini about 14 years
    +1, I figured that boost sleep() would be implemented with nanosleep(), good to have confirmation.
  • clemahieu
    clemahieu almost 13 years
    Where do you see sleep implemented with nanosleep? In thread.hpp I'm seeing: inline void sleep(xtime const& abs_time) { sleep(system_time(abs_time)); }
  • bames53
    bames53 almost 10 years
    One reason to use this_thread::sleep() is because it takes time in whatever units you like: sleep(seconds(10)), sleep(nanoseconds(10)), sleep(attoseconds(10)). The hardware resolution doesn't change, but whatever resolution is offered can be exposed without any need to keep introducing new sleep APIs (sleep, usleep, nanosleep, etc.).
  • Lightness Races in Orbit
    Lightness Races in Orbit over 8 years
    @clemahieu: That's not a call to ::sleep (otherwise you'd be limited to seconds resolution). It's a call to one of the many other boost::this_thread::sleep overloads, whose definition you may find in libs/thread/src/pthread/thread.cpp.
  • Gabriel Staples
    Gabriel Staples about 2 years
    It is true you have to use a while loop (or equivalent) to ensure you get the full sleep. For anyone looking for a robust demonstration of doing this with clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &wakeup_time, NULL) instead, with the TIMER_ABSTIME flag for absolute timestamps, see my answer and code examples here, in particular the code snippet starting with while (retcode == EINTR).