Sleeping a Boost.thread for some Boost.date_time nanoseconds
Good morning, I want a boost thread to sleep for some nanoseconds. The following code is a sample that compiles without errors. However, it does not work as expected and I cannot figure out why. It seems that the application get stacked when executes the instruction: sleep(time1) and it does not execute any thing else. #include <iostream> #include <boost/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time.hpp> //Building options: //-DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG -lboost_date_time-mt -lboost_thread-mt void replay() { boost::posix_time::time_duration time1, time2; time1=boost::posix_time::seconds(3); std::cout << boost::posix_time::to_simple_string(time1) << std::endl; boost::this_thread::sleep(time1); time2=boost::posix_time::nanoseconds(987654321); std::cout << boost::posix_time::to_simple_string(time2) << std::endl; boost::this_thread::sleep(time2); } int main(int argc, char* argv[]) { boost::thread replaythread(replay); replaythread.join(); return 0; } The BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is a preprocessor definition required in order to work with nanoseconds (http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/details.html#date_ti...). The thread sleeps perfectly for 3 seconds if you remove the mentioned definition from the building options and if you also comment out the code related to nanoseconds (lines 15-17). Thank you very much for your time This question was also posted to stackoverflow (http://stackoverflow.com/questions/6636731/) -- Emer RodrÃguez Formisano
On 10/07/11 10:53, Emer Rodriguez Formisano wrote:
Good morning,
I want a boost thread to sleep for some nanoseconds. The following code is a sample that compiles without errors. However, it does not work as expected and I cannot figure out why. It seems that the application get stacked when executes the instruction: sleep(time1) and it does not execute any thing else.
You mean stuck? Take a look at this ticket: https://svn.boost.org/trac/boost/ticket/5034 If this is the problem you are seeing please add a comment.
[snip]
Jamie
Thanks for the answer, Jamie.
You mean stuck? Take a look at this ticket: https://svn.boost.org/trac/boost/ticket/5034 Yes, sorry for the misspell and the poor description.
If this is the problem you are seeing please add a comment. It is not the same problem. My problems occurs when I set the -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG building option, then the boost::this_thread::sleep does not work for any posix::time_duration. The created thread uses all the CPU and it does not sleep nor process the remaining instructions. If the preprocessor definition is removed, the thread can sleep for any time period unless boost::posix_time::nanoseconds. The program uses some time_duration variables to store nanoseconds and that makes the boost::this_thread::sleep not work.
-- Emer RodrÃguez Formisano
participants (2)
-
Emer Rodriguez Formisano
-
Jamie Allsop