Class: Af::ThreadPool::Worker
- Inherits:
-
Object
- Object
- Af::ThreadPool::Worker
- Defined in:
- lib/fiksu-af/thread_pool.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
- #busy? ⇒ Boolean
- #get_block ⇒ Object
-
#initialize(thread_class = Thread) ⇒ Worker
constructor
A new instance of Worker.
- #reset_block ⇒ Object
- #set_block(block) ⇒ Object
Constructor Details
#initialize(thread_class = Thread) ⇒ Worker
Returns a new instance of Worker.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fiksu-af/thread_pool.rb', line 11 def initialize(thread_class = Thread) @mutex = Mutex.new @thread = thread_class.new do while true sleep 0.001 block = get_block if block block.call reset_block end end end end |
Instance Attribute Details
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
9 10 11 |
# File 'lib/fiksu-af/thread_pool.rb', line 9 def thread @thread end |
Instance Method Details
#busy? ⇒ Boolean
40 41 42 |
# File 'lib/fiksu-af/thread_pool.rb', line 40 def busy? @mutex.synchronize {!@block.nil?} end |
#get_block ⇒ Object
25 26 27 |
# File 'lib/fiksu-af/thread_pool.rb', line 25 def get_block @mutex.synchronize {@block} end |
#reset_block ⇒ Object
36 37 38 |
# File 'lib/fiksu-af/thread_pool.rb', line 36 def reset_block @mutex.synchronize {@block = nil} end |
#set_block(block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/fiksu-af/thread_pool.rb', line 29 def set_block(block) @mutex.synchronize do raise RuntimeError, "Thread already busy." if @block @block = block end end |