Class: ThreadPool::Worker

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

Overview

The Worker class starts a thread for performing work. A worker thread sleeps until it receives a block for processing, at which time it is considered busy. Once the block has been processed, the worker is considered available again.

Instance Method Summary collapse

Constructor Details

#initializeWorker

Starts the worker thread.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bulkmail/thread_pool.rb', line 19

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 true if the worker is busy processing a block.

Returns:

  • (Boolean)


50
51
52
# File 'lib/bulkmail/thread_pool.rb', line 50

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

#get_blockObject

Returns the block to be processed, if any.



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

def get_block
  @mutex.synchronize {@block}
end

#reset_blockObject

Resets the block instance variable to signify that the worker is available.



45
46
47
# File 'lib/bulkmail/thread_pool.rb', line 45

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

#set_block(block) ⇒ Object

Sets the block to be processed.



39
40
41
# File 'lib/bulkmail/thread_pool.rb', line 39

def set_block(block)
  @mutex.synchronize {@block = block}
end