Ruby 编程/参考/对象/线程/标准库/互斥锁
外观
Mutex 类(可通过 require 'thread' 加载)是一种确保特定块并发性的方法。
您可以在 1.9.2(或 jruby)中像这样超时等待
require 'thread'
a = Mutex.new
b = ConditionVariable.new
a.synchronize {
a.wait(b, 3) # timeout after 3 secs
}
在 1.8 非 jruby 中,您可以使用 timeout 库,例如
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