Boost logo

Boost :

From: William E. Kempf (wekempf_at_[hidden])
Date: 2003-02-25 10:26:30


Russell Hind said:
> Is yield intended to always yield to another thread if one can run?
> Because the code for yield is
>
> void thread::yield()
> {
> #if defined(BOOST_HAS_WINTHREADS)
> Sleep(0);
> #elif defined(BOOST_HAS_PTHREADS)
> # if defined(BOOST_HAS_SCHED_YIELD)
> int res = 0;
> res = sched_yield();
> assert(res == 0);
> # elif defined(BOOST_HAS_PTHREAD_YIELD)
> int res = 0;
> res = pthread_yield();
> assert(res == 0);
> # else
> xtime xt;
> xtime_get(&xt, TIME_UTC);
> sleep(xt);
> # endif
> #elif defined(BOOST_HAS_MPTASKS)
> MPYield();
> #endif
> }
>
> Taken from the main CVS.
>
> Sleep(0) on Win32 will only yield to another thread of equal or higher
> priority, not to lower priority threads.
>
> In boost::detail::lightweight_mutex::scoped_lock, it is mentioned that
> Sleep(1) will get around. Is the behaviour of Sleep(0) the intended use
> of yield?
>
> explicit scoped_lock(lightweight_mutex & m): m_(m)
> {
> while( InterlockedExchange(&m_.l_, 1) )
> {
> // Note: changed to Sleep(1) from Sleep(0).
> // According to MSDN, Sleep(0) will never yield
> // to a lower-priority thread, whereas Sleep(1)
> // will. Performance seems not to be affected.
>
> Sleep(1);
> }
> }
>
> (I don't actually use yield yet, so currently have no preference for
> either, but just wondered what the intended use of yield was)

I'll look into this and fix it. Thanks.

-- 
William E. Kempf

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk