How to sleep with boost::chrono?

2NinerRomeo picture 2NinerRomeo · Mar 3, 2012 · Viewed 11.6k times · Source

Examples of boost::this_thread::sleep() seem to use objects of boost::posix_time::milliseconds. I've tried that and it works, but I am using boost::chrono for checking the system clock etcetera. It seems to me I should be able to pass sleep() a chrono::duration like this:

boost::this_thread::sleep( boost::chrono::duration(10) );

But the compiler is giving me the following error:

... boost_1_49_0\boost/thread/win32/thread_data.hpp(171) : error C2039: 'total_milliseconds' : is not a member of 'boost::chrono::duration'

Which I find confusing. Am I right in thinking I should be able to do this? Will it be necessary to convert to a posix_time?

Answer

J.N. picture J.N. · Mar 3, 2012

Like this, use sleep_for and seconds

boost::this_thread::sleep_for( boost::chrono::seconds(10) );

EDIT

After verification this feature is not yet available in boost 1.49.0. All my apologies. This is only working in the trunk version of boost.

That means that it is not possible to call a sleep-like function without converting to a boost.datetime format.