I want the accelerating thread to sleep within a few nanoseconds. The following code is a sample that compiles without errors. However, it does not work properly, and I cannot understand why.
#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; }
BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is a preprocessor definition needed to work with nanoseconds ( more ). Problems arise when I set the -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG parameter, then boost :: this_thread :: sleep does not work for any posix :: time_duration. The created thread uses the entire processor, and it does not sleep or process the remaining instructions. If the preprocessor definition is removed, the thread can sleep for any period of time if boost :: posix_time :: nanoseconds. The program uses some time_duration variables to store nanoseconds, and this makes boost :: this_thread :: sleep inoperative.
Thanks so much for your time.
source share