Class: Concurrent::Event
- Inherits:
-
Object
- Object
- Concurrent::Event
- Defined in:
- lib/concurrent/event.rb
Overview
Old school kernel-style event reminiscent of Win32 programming in C++.
When an Event is created it is in the unset state. Threads can choose to #wait on the event, blocking until released by another thread. When one thread wants to alert all blocking threads it calls the #set method which will then wake up all listeners. Once an Event has been set it remains set. New threads calling #wait will return immediately. An Event may be #reset at any time once it has been set.
Instance Method Summary collapse
-
#initialize ⇒ Event
constructor
Creates a new
Eventin the unset state. -
#reset ⇒ Boolean
Reset a previously set event back to the
unsetstate. -
#set ⇒ Boolean
Trigger the event, setting the state to
setand releasing all threads waiting on the event. -
#set? ⇒ Boolean
Is the object in the set state?.
-
#wait(timeout = nil) ⇒ Boolean
Wait a given number of seconds for the
Eventto be set by another thread.
Constructor Details
#initialize ⇒ Event
Creates a new Event in the unset state. Threads calling #wait on the Event will block.
21 22 23 24 25 |
# File 'lib/concurrent/event.rb', line 21 def initialize @set = false @mutex = Mutex.new @condition = Condition.new end |
Instance Method Details
#reset ⇒ Boolean
Reset a previously set event back to the unset state. Has no effect if the Event has not yet been set.
54 55 56 57 58 59 60 |
# File 'lib/concurrent/event.rb', line 54 def reset @mutex.synchronize do @set = false end true end |
#set ⇒ Boolean
Trigger the event, setting the state to set and releasing all threads waiting on the event. Has no effect if the Event has already been set.
40 41 42 43 44 45 46 47 48 |
# File 'lib/concurrent/event.rb', line 40 def set @mutex.synchronize do return true if @set @set = true @condition.broadcast end true end |
#set? ⇒ Boolean
Is the object in the set state?
30 31 32 33 34 |
# File 'lib/concurrent/event.rb', line 30 def set? @mutex.synchronize do @set end end |
#wait(timeout = nil) ⇒ Boolean
Wait a given number of seconds for the Event to be set by another thread. Will wait forever when no timeout value is given. Returns immediately if the Event has already been set.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/concurrent/event.rb', line 67 def wait(timeout = nil) @mutex.synchronize do return true if @set remaining = Condition::Result.new(timeout) while !@set && remaining.can_wait? remaining = @condition.wait(@mutex, remaining.remaining_time) end @set end end |