Search code examples
c++multithreadingboostboost-thread

C++ boost thread delay


I want to wait 1.5 seconds in a boost thread. Using boost::xtime I can wait an integer number of seconds:

// Block on the queue / wait for data for up two seconds.
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec++;
xt.sec++;
....
_condition.timed_wait(_mutex, xt)

How can I wait 1.5 seconds instead?


Solution

  • Would the following not work using the nanoseconds and seconds portion and increasing by 0.5 billion nanoseconds and adding a second which is 1.5 seconds

    xt.sec++;
    xt.nsec += 500000000;
    _condition.timed_wait(_mutex, xt);