Class | ConditionVariable
In: | thread.rb
Parent: | Object

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

### Methods

broadcast new signal wait

### Public Class methods

new()

Creates a new ConditionVariable

[Source]

    # File thread.rb, line 192
      def initialize
        @waiters = []
      end

### Public Instance methods

broadcast()

Wakes up all threads waiting for this lock.

[Source]

    # File thread.rb, line 225
      def broadcast
        waiters0 = nil
        Thread.exclusive do
          waiters0 = @waiters.dup
          @waiters.clear
        end
        for t in waiters0
          begin
            t.run
          rescue ThreadError
          end
        end
      end

signal()

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

[Source]

    # File thread.rb, line 213
      def signal
        begin
          t = @waiters.shift
          t.run if t
        rescue ThreadError
          retry
        end
      end

wait(mutex)

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

[Source]

    # File thread.rb, line 199
      def wait(mutex)
        begin
          mutex.exclusive_unlock do
            @waiters.push(Thread.current)
            Thread.stop
          end
        ensure
          mutex.lock
        end
      end

[Validate]