Class: ThreadPool::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/thread_pool.rb

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/thread_pool.rb', line 6

def initialize
  @mutex = Mutex.new
  @thread = Thread.new do
    while true
      sleep 0.001
      block = get_block
      if block
        block.call
        reset_block
      end
    end
  end
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/thread_pool.rb', line 35

def busy?
  @mutex.synchronize {!@block.nil?}
end

#get_blockObject



20
21
22
# File 'lib/thread_pool.rb', line 20

def get_block
  @mutex.synchronize {@block}
end

#reset_blockObject



31
32
33
# File 'lib/thread_pool.rb', line 31

def reset_block
  @mutex.synchronize {@block = nil}
end

#set_block(block) ⇒ Object



24
25
26
27
28
29
# File 'lib/thread_pool.rb', line 24

def set_block(block)
  @mutex.synchronize do
    raise RuntimeError, "Thread already busy." if @block
    @block = block
  end
end