123456789_123456789_123456789_123456789_123456789_

Class: ConditionVariable

Relationships & Source Files
Inherits: Object
Defined in: thread_sync.c,
thread_sync.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:

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
   }
}

Class Method Summary

  • .new constructor

    Creates a new condition variable instance.

Instance Method Summary

Constructor Details

.new

Creates a new condition variable instance.

[ GitHub ]

  
# File 'thread_sync.c', line 1443

static VALUE
rb_condvar_initialize(VALUE self)
{
    struct rb_condvar *cv = condvar_ptr(self);
    list_head_init(&cv->waitq);
    return self;
}

Instance Method Details

#broadcast

Wakes up all threads waiting for this lock.

[ GitHub ]

  
# File 'thread_sync.c', line 1516

static VALUE
rb_condvar_broadcast(VALUE self)
{
    struct rb_condvar *cv = condvar_ptr(self);
    wakeup_all(&cv->waitq);
    return self;
}

#marshal_dump

This method is for internal use only.

Alias for Queue#marshal_dump.

#signal

Wakes up the first thread in line waiting for this lock.

[ GitHub ]

  
# File 'thread_sync.c', line 1502

static VALUE
rb_condvar_signal(VALUE self)
{
    struct rb_condvar *cv = condvar_ptr(self);
    wakeup_one(&cv->waitq);
    return self;
}

#wait(mutex, timeout = nil)

Releases the lock held in mutex and waits; reacquires the lock on wakeup.

If timeout is given, this method returns after timeout seconds passed, even if no other thread doesn’t signal.

[ GitHub ]

  
# File 'thread_sync.c', line 1475

static VALUE
rb_condvar_wait(int argc, VALUE *argv, VALUE self)
{
    rb_execution_context_t *ec = GET_EC();

    struct rb_condvar *cv = condvar_ptr(self);
    struct sleep_call args;

    rb_scan_args(argc, argv, "11", &args.mutex, &args.timeout);

    COROUTINE_STACK_LOCAL(struct sync_waiter, w);
    w->self = args.mutex;
    w->th = ec->thread_ptr;
    w->fiber = ec->fiber_ptr;

    list_add_tail(&cv->waitq, &w->node);
    rb_ensure(do_sleep, (VALUE)&args, delete_from_waitq, (VALUE)w);

    return self;
}