Class: ConditionVariable
- Inherits:
-
Object
- Object
- ConditionVariable
- Defined in:
- lib/thread.rb,
thread.c
Overview
ConditionVariable objects augment class Mutex. Using condition variables, it is possible to suspend while in the middle of a critical section until a resource becomes available.
Example:
require 'thread'
mutex = Mutex.new
resource = 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
}
}
Instance Method Summary collapse
-
#broadcast ⇒ Object
Wakes up all threads waiting for this condition.
-
#initialize ⇒ ConditionVariable
constructor
Creates a new ConditionVariable.
- #marshal_dump ⇒ Object
-
#marshal_load ⇒ Object
for marshalling mutexes and condvars.
-
#signal ⇒ Object
Wakes up the first thread in line waiting for this condition.
-
#wait ⇒ Object
Releases the lock held in
mutex
and waits; reacquires the lock on wakeup.
Constructor Details
#initialize ⇒ ConditionVariable
Creates a new ConditionVariable
187 188 189 |
# File 'lib/thread.rb', line 187 def initialize @waiters = [] end |
Instance Method Details
#broadcast ⇒ Object
Wakes up all threads waiting for this condition.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/thread.rb', line 220
/*
* Document-method: broadcast
* call-seq: broadcast
*
* Wakes up all threads waiting for this condition.
*
*/
static VALUE
rb_condvar_broadcast(VALUE self)
{
ConditionVariable *condvar;
Data_Get_Struct(self, ConditionVariable, condvar);
thread_exclusive(wake_all, (VALUE)&condvar->waiting);
rb_thread_schedule();
return self;
}
|
#marshal_dump ⇒ Object
#marshal_load ⇒ Object
for marshalling mutexes and condvars
|
# File 'thread.c'
/* for marshalling mutexes and condvars */
static VALUE
dummy_load(VALUE self, VALUE string)
{
return Qnil;
}
|
#signal ⇒ Object
Wakes up the first thread in line waiting for this condition.
208 209 210 211 212 213 214 215 |
# File 'lib/thread.rb', line 208 def signal begin t = @waiters.shift t.run if t rescue ThreadError retry end end |