Class: Fontisan::Utils::ThreadPool
- Inherits:
-
Object
- Object
- Fontisan::Utils::ThreadPool
- Defined in:
- lib/fontisan/utils/thread_pool.rb
Overview
Simple thread pool implementation
Manages a fixed number of worker threads for parallel job execution. Jobs are queued and processed by available workers.
Instance Method Summary collapse
-
#initialize(size) ⇒ ThreadPool
constructor
Initialize thread pool.
-
#schedule { ... } ⇒ Future
Schedule a job for execution.
-
#shutdown ⇒ Object
Shutdown thread pool.
Constructor Details
#initialize(size) ⇒ ThreadPool
Initialize thread pool
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fontisan/utils/thread_pool.rb', line 19 def initialize(size) @size = size @queue = Queue.new @threads = [] @shutdown = false # Start worker threads @size.times do @threads << Thread.new { worker_loop } end end |
Instance Method Details
#schedule { ... } ⇒ Future
Schedule a job for execution
35 36 37 38 39 |
# File 'lib/fontisan/utils/thread_pool.rb', line 35 def schedule(&block) future = Future.new @queue << { block: block, future: future } future end |
#shutdown ⇒ Object
Shutdown thread pool
Waits for all queued jobs to complete and stops workers.
44 45 46 47 48 49 50 51 52 |
# File 'lib/fontisan/utils/thread_pool.rb', line 44 def shutdown @shutdown = true # Signal all threads to stop @size.times { @queue << :stop } # Wait for all threads to finish @threads.each(&:join) end |