Boost logo

Boost Users :

From: Raúl Huertas (raulh39_at_[hidden])
Date: 2006-12-12 15:32:48


Forever Kid escribió:
> What is the resolution of the boost sleep() function?
>
> ...
> What about sleeping for ~100 millseconds?
(Excuse my english :*))
Do you mean 100 milliseconds ( 100 * 10^-3)? I will assume that.
> How is that accomplished?
Here:
http://www.boost.org/doc/html/xtime.html
it says: "For maximum portability, avoid durations of less than one second."

I've never tried to sleep a thread for less than a second, but it seems
that the resolution is implementation specific...

>
> Does this work?
>
> xTime.nsec += 100000;
> boost::thread::sleep(xTime);
>
"nsec" stands for "nanosecond", doesn't it?

So 100 milliseconds is 100000000 nanoseconds:
xTime.nsec += 100000000;

Also, you have to account for the possible overflow:

const int NANOSECONDS_PER_SECOND = 1000000000;
if(xTime.nsec>=NANOSECONDS_PER_SECOND)
{
  xTime.nsec -= NANOSECONDS_PER_SECOND;
  xTime.sec += 1;
}

Perhaps you find useful the function delay() that you can find in
libs/thread/test/util.inl of your Boost 1.33.1 distribution:

inline boost::xtime delay(int secs, int msecs=0, int nsecs=0)
{
    const int MILLISECONDS_PER_SECOND = 1000;
    const int NANOSECONDS_PER_SECOND = 1000000000;
    const int NANOSECONDS_PER_MILLISECOND = 1000000;

    boost::xtime xt;
    if (boost::TIME_UTC != boost::xtime_get (&xt, boost::TIME_UTC))
        BOOST_ERROR ("boost::xtime_get != boost::TIME_UTC");

    nsecs += xt.nsec;
    msecs += nsecs / NANOSECONDS_PER_MILLISECOND;
    secs += msecs / MILLISECONDS_PER_SECOND;
    nsecs += (msecs % MILLISECONDS_PER_SECOND) *
NANOSECONDS_PER_MILLISECOND;
    xt.nsec = nsecs % NANOSECONDS_PER_SECOND;
    xt.sec += secs + (nsecs / NANOSECONDS_PER_SECOND);

    return xt;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net