class SimplePool
Public Class Methods
new(thread_num)
click to toggle source
# File lib/simple-pool.rb, line 4 def initialize(thread_num) @kill = false @stop = false @q = Queue.new @m = Mutex.new @cv = ConditionVariable.new @threads = [] thread_num.times do |i| @threads << Thread.new{round_n_round} end end
Public Instance Methods
invoke(&callback)
click to toggle source
# File lib/simple-pool.rb, line 16 def invoke(&callback) @q << callback @m.synchronize{ @cv.broadcast } end
kill_right_now()
click to toggle source
# File lib/simple-pool.rb, line 21 def kill_right_now @kill = true @m.synchronize{ @cv.broadcast } @threads.each{|t| t.join} end
wait_until_finish()
click to toggle source
# File lib/simple-pool.rb, line 27 def wait_until_finish @stop = true @m.synchronize{ @cv.broadcast } @threads.each{|t| t.join} end
Private Instance Methods
do_it()
click to toggle source
# File lib/simple-pool.rb, line 34 def do_it begin callback = @q.pop callback.call rescue end end
round_n_round()
click to toggle source
# File lib/simple-pool.rb, line 43 def round_n_round until @kill do unless @q.empty? do_it next end if @stop @kill = true next end @m.synchronize{ @cv.wait(@m, 1) } end end