class ConditionVariable
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 } }
Public Instance Methods
broadcast
click to toggle source
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()
click to toggle source
static VALUE dummy_dump(VALUE self) { return rb_str_new2(""); }
marshal_load(p1)
click to toggle source
for marshalling mutexes and condvars
static VALUE dummy_load(VALUE self, VALUE string) { return Qnil; }
signal
click to toggle source
Wakes up the first thread in line waiting for this condition.
static VALUE rb_condvar_signal(VALUE self) { ConditionVariable *condvar; Data_Get_Struct(self, ConditionVariable, condvar); signal_condvar(condvar); return self; }
wait
click to toggle source
Releases the lock held in mutex
and waits; reacquires the lock
on wakeup.
static VALUE rb_condvar_wait(VALUE self, VALUE mutex_v) { ConditionVariable *condvar; Data_Get_Struct(self, ConditionVariable, condvar); if (CLASS_OF(mutex_v) != rb_cMutex) { /* interoperate with legacy mutex */ legacy_wait_args args; args.condvar = condvar; args.mutex = mutex_v; rb_iterate(legacy_exclusive_unlock, mutex_v, legacy_wait, (VALUE)&args); } else { Mutex *mutex; Data_Get_Struct(mutex_v, Mutex, mutex); wait_condvar(condvar, mutex); } return self; }