411 REFERENCE · Wikibooks
Ruby Programming/Reference/Objects/Thread/Standard Library/Mutex
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Mutex
The Mutex class (loadable via require 'thread') is a way of guaranteeing concurrency for a specific block.
Timing out a wait
You can time out a wait like this in 1.9.2 (or jruby)
require 'thread'
a = Mutex.new
b = ConditionVariable.new
a.synchronize {
a.wait(b, 3) # timeout after 3 secs
}
In 1.8 non jruby, you can use the timeout library, like
require 'thread'
require 'timeout'
a = Mutex.new
b = ConditionVariable.new
begin
Timeout::timeout(3) {
a.synchronize {
a.wait(b)
}
}
rescue Timeout::Error
# timed out
end
References
a tutorial