C++ Letting a boost thread wait for 1 second

19,020

Solution 1

Is there an easier way

Maybe something along these lines:

boost::this_thread::sleep(boost::posix_time::seconds(1));

boost::thread::sleep(boost::posix_time::seconds(1));

Solution 2

boost::xtime_get() looks like one of the few Boost APIs that's not implemented in a header, so this might be something like not having the Boost library compiled correctly. This is probably somelike having mismatched calling conventions or something. I don't know off the top of my head what steps you might need to go through to rebuild the library - all I've ever used in Boost has been stuff that only requires the headers.

It might be helpful if you just trace into the xtime_get() routine, even if it's at the assembly level. The xtime struct is very, very basic and xtime_get() really doesn't do anything more than call a platform-specific API to get the numbers to plug into the xtime struct.

Share:
19,020
Admin
Author by

Admin

Updated on July 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I have created a boost thread using: boost::thread thrd(&connectionThread); where connectionThread is a simple void function. This works fine, however, when I try to make it wait for some seconds, for example using:

    boost::xtime xt;
    
    boost::xtime_get(&xt, boost::TIME_UTC);
    
    xt.sec += 1;
    
    boost::thread::sleep(xt); // Sleep for 1 second
    

    The program crashes at the xtime_get line. Even when manually trying to set xt.sec it doesn't work. I've tried several other methods, but I can't seem to make it work. Is there something I'm doing wrong? Is there a easier way to achieve my goal?

  • MiniScalope
    MiniScalope over 13 years
    your second way (what i have tryied first) doesnt work... the first one is fine