|
Boost Users : |
From: Kutch, Patrick G (patrick.g.kutch_at_[hidden])
Date: 2004-07-22 22:06:30
I am used to putting in quick sleep() statements in my worker threads,
usually a sleep(0) just to give up a timeslice, or maybe a sleep(1).
Of course boost::thread::sleep takes an xtime object not a milliseconds
value, so I wrote a small routine to simulate the sleep() statement
using boost::thread::sleep:
void inline UtilitySleep(long milliSeconds)
{
const int NANOSECONDS_PER_MILLISECOND = 1000000;
if (0 == milliSeconds)
{
boost::thread::yield(); // just do a yield
return;
}
boost::xtime time;
int sec = 0;
// if larger than 1 sec, do some voodoo for the boost::xtime struct
if (milliSeconds >= 1000)
{ // convert ms > 1000 into secs + remaining ms
int secs = milliSeconds /1000;
milliSeconds = milliSeconds - secs*1000;
sec += secs;
}
milliSeconds*=NANOSECONDS_PER_MILLISECOND;
// normal boost time sleep stuff
// get current time
boost::xtime_get(&time,boost::TIME_UTC);
// add # of desired secs and ms
time.nsec+=milliSeconds;
time.sec+= sec;
// sleep until that time
boost::thread::sleep(time);
}
It seems to always work fine unde Windows, however occasionally under
Linux it appears that if I use a small value, such as sleep(2) that
sometimes it takes longer to setup the values and call
boost::thread:sleep() and I end up passing an xtime value that has
already passed, basically blocking the thread forever.
Anybody have any advice for me?
Thanx!
- Patrick
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