8 #ifndef Sawyer_WorkList_H
9 #define Sawyer_WorkList_H
11 #include <Sawyer/Synchronization.h>
24 mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
33 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
34 items_.push_back(item);
39 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
40 return items_.empty();
52 SAWYER_THREAD_TRAITS::LockGuard lock(mutex_);
53 ASSERT_forbid(items_.empty());
54 Item item = items_.front();
68 template<
class WorkItems,
class Functor>
70 #if SAWYER_MULTI_THREADED
72 boost::condition_variable cond;
73 size_t nActiveWorkers = 0;
76 maxWorkers = boost::thread::hardware_concurrency();
77 ASSERT_require(maxWorkers > 0);
81 boost::unique_lock<boost::mutex> lock(mutex);
82 while (!workList.isEmpty() && nActiveWorkers >= maxWorkers)
86 while (!workList.isEmpty() && nActiveWorkers < maxWorkers) {
89 static void doWork(Functor f,
const typename WorkItems::Item &item, WorkItems *workList,
90 boost::mutex *mutex, boost::condition_variable *cond,
size_t *nActiveWorkers) {
92 boost::lock_guard<boost::mutex> lock(*mutex);
98 boost::thread thrd(Worker::doWork, f, workList.next(), &workList, &mutex, &cond, &nActiveWorkers);
104 if (workList.isEmpty() && 0 == nActiveWorkers)
108 while (!workList.isEmpty())
109 f(workList.next(), workList);
First-in-first-out work queue.
void insert(const Item &item)
Insert one unit of work into the queue.
void processWorkList(WorkItems &workList, size_t maxWorkers, Functor f)
Process work from a work list in parallel.
T Item
The type that represents one unit of work.
Item next()
Remove and return the next item of work.
Name space for the entire library.
bool isEmpty() const
Test whethere the queue is empty.