类 Thread::ConditionVariable

ConditionVariable 对象增强了 Mutex 类。使用条件变量,可以在临界区中挂起,直到资源可用为止。

示例

mutex = Thread::Mutex.new
resource = Thread::ConditionVariable.new

a = Thread.new {
   mutex.synchronize {
     # Thread 'a' now needs the resource
     resource.wait(mutex)
     # 'a' can now have the resource
   }
}

b = Thread.new {
   mutex.synchronize {
     # Thread 'b' has finished using the resource
     resource.signal
   }
}