Class: Listen::Turnstile
- Inherits:
-
Object
- Object
- Listen::Turnstile
- Defined in:
- lib/listen/turnstile.rb
Overview
Note:
Only two threads can be used with this Turnstile because of the current implementation.
Allows two threads to wait on eachother.
Instance Attribute Summary collapse
-
#queue ⇒ Object
Returns the value of attribute queue.
Instance Method Summary collapse
-
#initialize ⇒ Turnstile
constructor
Initialize the turnstile.
-
#signal ⇒ Object
Unblocks the waiting thread if any.
-
#wait ⇒ Object
Blocks the current thread until a signal is received.
Constructor Details
#initialize ⇒ Turnstile
Initialize the turnstile.
12 13 14 15 16 |
# File 'lib/listen/turnstile.rb', line 12 def initialize # Until Ruby offers semahpores, only queues can be used # to implement a turnstile. @queue = Queue.new end |
Instance Attribute Details
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/listen/turnstile.rb', line 8 def queue @queue end |
Instance Method Details
#signal ⇒ Object
Unblocks the waiting thread if any.
26 27 28 |
# File 'lib/listen/turnstile.rb', line 26 def signal queue.push(:dummy) if queue.num_waiting == 1 end |
#wait ⇒ Object
Blocks the current thread until a signal is received.
20 21 22 |
# File 'lib/listen/turnstile.rb', line 20 def wait queue.pop if queue.num_waiting == 0 end |