Class: Ruactor::Dispatcher
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
-
#initialize ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize ⇒ Dispatcher
Returns a new instance of Dispatcher.
19 20 21 22 23 |
# File 'lib/ruactor.rb', line 19 def initialize() @queue = Queue.new @started = false @lock = Mutex.new end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
17 18 19 |
# File 'lib/ruactor.rb', line 17 def queue @queue end |
Instance Method Details
#start ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruactor.rb', line 25 def start @lock.synchronize { unless started? @threads = [] 5.times do thread = Thread.new do loop do obj, method, args, block = @queue.pop obj.send method, *args, &block end end @threads << thread end @started = true end } end |
#started? ⇒ Boolean
51 52 53 |
# File 'lib/ruactor.rb', line 51 def started? @started end |
#stop ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/ruactor.rb', line 43 def stop @lock.synchronize { @threads.each { |t| t.exit } @threads.each { |t| t.join } @started = false } end |
#stopped? ⇒ Boolean
55 56 57 |
# File 'lib/ruactor.rb', line 55 def stopped? !started? end |