Search code examples
c++boostsleepboost-thread

Whats the difference between using boost::this_thread::sleep_for() and regular sleep() function?


We know that boost::this_thread::sleep_for(...) can be used for putting the currently running thread into sleep. How different it is from the regular sleep() function. It seems everyplace where we use boost::this_thread::sleep_for(...) can be simply replaced by sleep() method without affecting the results. Can anyone throw some light into it.


Solution

  • C++ didn't provide a sleep function until C++11 came along, which offers std::thread::sleep_for(). So Boost provides its own for making your code platform-independent.

    The C functions sleep(), usleep(), and Sleep() are platform-specific rather than part of the C++ standard library.