123456789_123456789_123456789_123456789_123456789_

Class: Thread::Monitor::ConditionVariable

Relationships & Source Files
Inherits: Object
Defined in: thread_sync.rb

Overview

Condition variables, allow to suspend the current thread while in the middle of a critical section until a condition is met, such as a resource being available.

Example:

  monitor = Thread::Monitor.new

  resource_available = false
  condvar = monitor.new_cond

  a1 = Thread.new {
    # Thread 'a1' waits for the resource to become available and consumes
    # the resource.
    monitor.synchronize {
      condvar.wait_until { resource_available }
      # After the loop, 'resource_available' is guaranteed to be true.

      resource_available = false
      puts "a1 consumed the resource"
    }
  }

  a2 = Thread.new {
    # Thread 'a2' behaves like 'a1'.
    monitor.synchronize {
      condvar.wait_until { resource_available }
      resource_available = false
      puts "a2 consumed the resource"
    }
  }

  b = Thread.new {
    # Thread 'b' periodically makes the resource available.
    loop {
      monitor.synchronize {
        resource_available = true

        # Notify one waiting thread if any.  It is possible that neither
        # 'a1' nor 'a2 is waiting on 'condvar' at this moment.  That's OK.
        condvar.signal
      }
      sleep 1
    }
  }

  # Eventually both 'a1' and 'a2' will have their resources, albeit in an
  # unspecified order.
  [a1, a2].each {|th| th.join}

Class Method Summary

Instance Method Summary

  • #broadcast

    Wakes up all threads waiting for this lock.

  • #signal

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

  • #wait(timeout = nil)

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

  • #wait_until

    Calls wait repeatedly until the given block yields a truthy value.

  • #wait_while

    Calls wait repeatedly while the given block yields a truthy value.

Constructor Details

.new(monitor) ⇒ ConditionVariable

This method is for internal use only.
[ GitHub ]

  
# File 'thread_sync.rb', line 659

def initialize(monitor) # :nodoc:
  @monitor = monitor
  @cond = Thread::ConditionVariable.new
end

Instance Method Details

#broadcast

Wakes up all threads waiting for this lock.

[ GitHub ]

  
# File 'thread_sync.rb', line 703

def broadcast
  @monitor.mon_check_owner
  @cond.broadcast
end

#signal

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

[ GitHub ]

  
# File 'thread_sync.rb', line 695

def signal
  @monitor.mon_check_owner
  @cond.signal
end

#wait(timeout = nil)

Releases the lock held in the associated monitor 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.rb', line 669

def wait(timeout = nil)
  @monitor.mon_check_owner
  @monitor.wait_for_cond(@cond, timeout)
end

#wait_until

Calls wait repeatedly until the given block yields a truthy value.

[ GitHub ]

  
# File 'thread_sync.rb', line 686

def wait_until
  until yield
    wait
  end
end

#wait_while

Calls wait repeatedly while the given block yields a truthy value.

[ GitHub ]

  
# File 'thread_sync.rb', line 677

def wait_while
  while yield
    wait
  end
end