2 #include <boost/thread.hpp>
8 :_thread_count(0), _min_threads(1), _max_threads(50), _running(true)
10 for (int i = 0; i < _min_threads; ++i)
16 WorkQueue::WorkQueue(int thread_count)
18 _min_threads(1), _max_threads(thread_count), _running(true)
20 for (int i = 0; i < _min_threads; ++i)
26 WorkQueue::~WorkQueue()
31 mutex::scoped_lock lock(_queue_lock);
32 _queue_non_empty.notify_all();
38 void WorkQueue::spawnThread()
41 _threads.create_thread(Worker(this));
44 shared_ptr<WorkUnit> WorkQueue::get()
46 mutex::scoped_lock lock(_queue_lock);
48 while (_queue.size() == 0)
50 _queue_non_empty.wait(lock);
51 if (!_running) throw interrupted_error();
54 shared_ptr<WorkUnit> back = _queue.front();
57 if (_queue.size() > 0 && _thread_count < _max_threads) spawnThread();
62 void WorkQueue::put(shared_ptr<WorkUnit> work_unit)
64 mutex::scoped_lock lock(_queue_lock);
66 _queue.push(work_unit);
68 _queue_non_empty.notify_one();
71 WorkQueue::Worker::Worker(WorkQueue* queue)
76 void WorkQueue::Worker::operator()()
82 shared_ptr<WorkUnit> unit = _queue->get();
86 catch (interrupted_error)
93 TaskNotification::TaskNotification()
94 :_expected(0), _count(0), _fail_count(0)
98 void TaskNotification::registerTask()
100 mutex::scoped_lock lock(_lock);
104 void TaskNotification::completeTask(bool success)
106 mutex::scoped_lock lock(_lock);
107 if (!success) ++_fail_count;
108 if (++_count == _expected) _cond.notify_all();
111 void TaskNotification::waitForComplete()
113 mutex::scoped_lock lock(_lock);
114 while (_count < _expected)
120 bool TaskNotification::failCount()
125 } // namespace bicker