#include #include "thread_pool.hpp" thread_pool::thread_pool(unsigned short threadcount) : running(true) { while(threadcount--) this->threads.create_thread(boost::bind(&thread_pool::worker, this)); } thread_pool::~thread_pool() { this->running=false; this->work_condition.notify_all(); this->threads.join_all(); } void thread_pool::worker() { while(true) { function_type f; { boost::mutex::scoped_lock lock(this->work_mutex); this->work_condition.wait(lock); if(!this->running) break; f=this->work.front(); this->work.pop(); } try { f(); } catch(...) { // try to keep thread alive.. } } } void thread_pool::queue(const function_type &fn) { boost::mutex::scoped_lock lock(this->work_mutex); this->work.push(fn); this->work_condition.notify_one(); }