Class: SimplePool

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(thread_num) ⇒ SimplePool

Returns a new instance of SimplePool.



4
5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#invoke(&callback) ⇒ Object



16
17
18
19
# File 'lib/simple-pool.rb', line 16

def invoke(&callback)
    @q << callback
    @m.synchronize{ @cv.broadcast }
end

#kill_right_nowObject



21
22
23
24
25
# 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_finishObject



27
28
29
30
31
# File 'lib/simple-pool.rb', line 27

def wait_until_finish
    @stop = true
    @m.synchronize{ @cv.broadcast }
    @threads.each{|t| t.join}
end