RE: [Boost-users] Looking for some help with boost::thread::sleep()
Has nobody else experienced this? Patrick Kutch Life is too short to drink bad coffee. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Kutch, Patrick G Sent: Thursday, July 22, 2004 8:07 PM To: boost-users@lists.boost.org Subject: [Boost-users] Looking for some help with boost::thread::sleep() 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Kutch, Patrick G wrote:
Has nobody else experienced this?
Sorry, I forgot all about answering your original post. If you look at the thread::sleep() method, it looks like the following; can you tell me which path it's taking (there are several options for pthreads) when you have the problem? void thread::sleep(const xtime& xt) { for (int foo=0; foo < 5; ++foo) { #if defined(BOOST_HAS_WINTHREADS) int milliseconds; to_duration(xt, milliseconds); Sleep(milliseconds); #elif defined(BOOST_HAS_PTHREADS) # if defined(BOOST_HAS_PTHREAD_DELAY_NP) timespec ts; to_timespec_duration(xt, ts); int res = 0; res = pthread_delay_np(&ts); assert(res == 0); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; to_timespec_duration(xt, ts); // nanosleep takes a timespec that is an offset, not // an absolute time. nanosleep(&ts, 0); # else mutex mx; mutex::scoped_lock lock(mx); condition cond; cond.timed_wait(lock, xt); # endif #elif defined(BOOST_HAS_MPTASKS) int microseconds; to_microduration(xt, microseconds); Duration lMicroseconds(kDurationMicrosecond * microseconds); AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds)); threads::mac::detail::safe_delay_until(&sWakeTime); #endif xtime cur; xtime_get(&cur, TIME_UTC); if (xtime_cmp(xt, cur) <= 0) return; } } Mike
participants (2)
-
Kutch, Patrick G
-
Michael Glassford