SimpleThreadPool
Simple implementation of the thread pool to manage executing tasks in parallel. The thread pool implemented by this code is designed to allow throttled, parallel execution of tasks.
Unlike some thread pools, this code does not maintain an internal queue of tasks. Instead, it simply blocks until a thread is available. This ensures that the pool memory size doesn't become bloated with 1000's of enqueued blocks of code.
The threads created for running the tasks are short lived threads and are not reused by the pool. This ensures that no thread local variables can accidentally be reused between threads.
Installation
Add this line to your application's Gemfile:
gem 'simple_thread_pool'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_thread_pool
Usage
thread_pool = SimpleThreadPoolnew(10)
thread_pool.execute do
end
thread_pool.execute("foo") do
end
thread_pool.finish
Error handling.
All error handling must be conducted inside the execute block. The main thread will not be notified of any exceptions. You can use the synchronized
method on the thread pool if you need to work data from the main thread.
Example of how you can track any errors in a shared array.
errors = []
thread_pool.execute do
begin
rescue Error => e
thread_pool.synchronized { errors << e }
raise e
end
end
thread_pool.finish
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bdurand/simple_thread_pool.
License
The gem is available as open source under the terms of the MIT License.