class Listen::Turnstile

Allows two threads to wait on eachother.

@note Only two threads can be used with this Turnstile

because of the current implementation.

Public Class Methods

new() click to toggle source

Initialize the turnstile.

# File lib/listen/turnstile.rb, line 10
def initialize
  # Until ruby offers semahpores, only queues can be used
  # to implement a turnstile.
  @q = Queue.new
end

Public Instance Methods

signal() click to toggle source

Unblocks the waiting thread if there is one.

# File lib/listen/turnstile.rb, line 24
def signal
  @q.push :dummy if @q.num_waiting == 1
end
wait() click to toggle source

Blocks the current thread until a signal is received.

# File lib/listen/turnstile.rb, line 18
def wait
  @q.pop if @q.num_waiting == 0
end