Class: Rubee::ThreadPool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubee/async/thread_pool.rb

Constant Summary collapse

THREADS_LIMIT =
Rubee::Configuration.get_threads_limit || 4
FIBERS_LIMIT =
Rubee::Configuration.get_fibers_limit || 4

Instance Method Summary collapse

Constructor Details

#initializeThreadPool

Returns a new instance of ThreadPool.



9
10
11
12
13
14
15
16
# File 'lib/rubee/async/thread_pool.rb', line 9

def initialize
  @tasks = Queue.new
  @threads = []
  @running = true
  @mutex = Mutex.new

  spawn_workers
end

Instance Method Details

#enqueue(task, args = {}) ⇒ Object



18
19
20
# File 'lib/rubee/async/thread_pool.rb', line 18

def enqueue(task, args = {})
  @tasks << [task, args]
end

#shutdownObject



22
23
24
25
26
# File 'lib/rubee/async/thread_pool.rb', line 22

def shutdown
  @running = false
  THREADS_LIMIT.times { @tasks << :shutdown } # Unblock threads
  @threads.each(&:join)
end