Bug #2629

ConditionVariable#wait(mutex, timeout) should return whether the condition was signalled, not the waited time

Added by hongli (Hongli Lai) almost 11 years ago. Updated over 9 years ago.

Target version:
ruby -v:
ruby 1.9.2dev (2010-01-21 trunk 26367) [x86_64-darwin10.2.0]


At this time, ConditionVariable#wait on 1.9.2dev works as follows:

ConditionVariable#wait(mutex, timeout) => integer
Waits for at most 'timeout' time. 'timeout' may be a floating
point number.
Returns the number of seconds spent waiting. So even if it
actually spent 3.5 seconds waiting, it'll either return 3 or 4.
There is no way to check whether the wait was signaled or
timed out, you have to guess based on the time waited.

Ideally it should just return true or false, just like how JRuby's version behaves:

ConditionVariable#wait(mutex, timeout) => boolean
Waits for at most 'timeout' time. 'timeout' may be a floating
point number.
Returns true if condition was signaled, false if it timed out.


I have an application which uses ConditionVariable#wait(mutex, timeout) in order to wait on a condition for a bounded time. The use case is as follows:

There is a background thread which performs some cleaning function every x seconds. We also want to be able to tell the thread to clean now, or to exit (i.e. quitting its main loop so that we can join the thread). This thread waits on a condition variable for x seconds, and then checks whether there was a timeout on the wait, or whether the wait as signaled. In case of the former it will run the cleanup code. In case of the latter it'll check whether @quit is set, and then either stop the loop or run the cleanup code.

The problem with 1.9.2dev's return value is that there is no way for me to detect whether the condition was signaled or whether the wait timed out.
I can try to guess whether there was a timeout by checking whether the waited time is >= the timeout, but the return value doesn't allow me to do even that because it has seconds resolution. Right now I have to work around it by placing timers around the #wait call, so that I can measure the difference in time in miliseconds or microseconds.

Please make ConditionVariable#wait(mutex, timeout) behave like JRuby's version.

Also available in: Atom PDF