indexing description: "A pool of worker threads." author: "Patrick Ruckstuhl " date: "$Date$" revision: "$Revision$" class WORKER_POOL create make feature {NONE} -- Creation make (a_pool_size: like pool_size; a_job_queue: like job_queue) is -- Create with a_pool_size and a_job_queue. require a_pool_size_ok: a_pool_size > 0 a_job_queue_ok: a_job_queue /= Void local i: NATURAL l_thread: WORKER_THREAD do pool_size := a_pool_size job_queue := a_job_queue create worker_threads.make (pool_size) from i := 1 until i > pool_size loop create l_thread.make (agent execute_worker_jobs) worker_threads.force (l_thread) i := i + 1 end ensure pool_size_set: pool_size = a_pool_size job_queue_set: job_queue = a_job_queue end feature -- Status is_stop_worker: BOOLEAN -- Should worker threads be stopped? feature -- Access pool_size: NATURAL_16 -- Number of worker threads in pool. job_queue: JOB_QUEUE -- Job queue the worker pool is working on. feature -- Commands start is -- Start the worker threads. do -- Launch worker threads is_stop_worker := False from worker_threads.start until worker_threads.after loop worker_threads.item.launch worker_threads.forth end end stop is -- Stop the worker threads. local i: NATURAL_16 do -- stop worker threads is_stop_worker := True -- for each thread we have to add a job that lifts the lock and lets it check against is_stop_worker from i := 1 until i > pool_size loop job_queue.force (agent do_nothing) i := i + 1 end -- now join each thread from worker_threads.start until worker_threads.after loop worker_threads.item.join worker_threads.forth end end feature {NONE} -- Implementation worker_threads: ARRAYED_LIST [THREAD] -- Worker threads executing jobs in job_queue. execute_worker_jobs is -- Execute jobs from worker_jobs. local l_retried: BOOLEAN do if not l_retried then from until is_stop_worker loop job_queue.execute end end rescue -- TODO we should also log the error retry end invariant pool_size_ok: pool_size > 0 job_queue_not_void: job_queue /= Void worker_threads_not_void: worker_threads /= Void worker_threads_number_ok: worker_threads.count = pool_size end