Class: Arb::Thread::TaskDispatcher

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

Overview

Simple thread pool.

Instance Method Summary collapse

Constructor Details

#initialize(max_thread_count) ⇒ TaskDispatcher

Returns a new instance of TaskDispatcher.



7
8
9
10
11
12
# File 'lib/arb/thread.rb', line 7

def initialize(max_thread_count)
  @assist_mutex=Mutex.new
  @task_mutex=Mutex.new
  @max_thread_count=max_thread_count
  @current_thread_count=0
end

Instance Method Details

#availableObject



39
40
41
42
43
# File 'lib/arb/thread.rb', line 39

def available
  task_sync do
    @current_thread_count
  end
end

#new_task(refresh_delay = 0.2, &blk) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/arb/thread.rb', line 45

def new_task(refresh_delay=0.2, &blk)
  return nil unless blk
  break_loop=false
  loop do
    task_sync do
      if @current_thread_count<@max_thread_count
        break_loop=true
        task_start(&blk)
      end
    end
    break if break_loop
    sleep refresh_delay
  end
end