Class: Amigo::ThreadingEvent
- Inherits:
-
Object
- Object
- Amigo::ThreadingEvent
- Defined in:
- lib/amigo/threading_event.rb
Overview
Threading event on Concurrent::Event, ManualResetEvent, etc. Efficient way to sleep and wake up.
Instance Method Summary collapse
-
#initialize(initial = false) ⇒ ThreadingEvent
constructor
A new instance of ThreadingEvent.
-
#reset ⇒ Object
Reset the event back to its original state.
-
#set ⇒ Object
Signal the event.
-
#set? ⇒ Boolean
True if
sethas been called. -
#wait(timeout = nil) ⇒ Object
Sleep the current thread until
setis called by another thread.
Constructor Details
#initialize(initial = false) ⇒ ThreadingEvent
Returns a new instance of ThreadingEvent.
7 8 9 10 11 |
# File 'lib/amigo/threading_event.rb', line 7 def initialize(initial=false) @mutex = Mutex.new @cv = ConditionVariable.new @signaled = initial end |
Instance Method Details
#reset ⇒ Object
Reset the event back to its original state.
36 37 38 39 40 41 |
# File 'lib/amigo/threading_event.rb', line 36 def reset # _debug("reset") @mutex.synchronize do @signaled = false end end |
#set ⇒ Object
Signal the event. The waiting threads will wake up.
24 25 26 27 28 29 30 |
# File 'lib/amigo/threading_event.rb', line 24 def set # _debug("set") @mutex.synchronize do @signaled = true @cv.broadcast # wake up all waiters end end |
#set? ⇒ Boolean
True if set has been called.
33 |
# File 'lib/amigo/threading_event.rb', line 33 def set? = @signaled |
#wait(timeout = nil) ⇒ Object
Sleep the current thread until set is called by another thread.
16 17 18 19 20 21 |
# File 'lib/amigo/threading_event.rb', line 16 def wait(timeout=nil) # _debug("wait") @mutex.synchronize do @cv.wait(@mutex, timeout) end end |