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 Method Summary collapse
-
#initialize ⇒ Turnstile
constructor
Initialize the turnstile.
-
#signal ⇒ Object
Unblocks the waiting thread if there is one.
-
#wait ⇒ Object
Blocks the current thread until a signal is received.
Constructor Details
#initialize ⇒ Turnstile
Initialize the turnstile.
10 11 12 13 14 |
# 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 |
Instance Method Details
#signal ⇒ Object
Unblocks the waiting thread if there is one.
24 25 26 |
# File 'lib/listen/turnstile.rb', line 24 def signal @q.push :dummy if @q.num_waiting == 1 end |
#wait ⇒ Object
Blocks the current thread until a signal is received.
18 19 20 |
# File 'lib/listen/turnstile.rb', line 18 def wait @q.pop if @q.num_waiting == 0 end |