WorkQueue.h 949 Bytes
Newer Older
1 2 3 4 5 6 7 8
#ifndef WORKQUEUE_H_
#define WORKQUEUE_H_

#include <common/components/worker/Work.h>
#include <common/threading/Mutex.h>
#include <common/threading/Condition.h>
#include <common/Common.h>

9 10
#include <mutex>

11 12 13 14 15 16 17 18 19
class WorkQueue
{
   public:
      WorkQueue() {}
      ~WorkQueue()
      {
         for(WorkListIter iter = workList.begin(); iter != workList.end(); iter++)
            delete(*iter);
      }
20

21 22
      Work* waitForNewWork()
      {
23 24
         std::lock_guard<Mutex> mutexLock(mutex);

25 26
         while(workList.empty() )
            newWorkCond.wait(&mutex);
27

28 29
         Work* work = *workList.begin();
         workList.pop_front();
30

31 32
         return work;
      }
33

34 35
      void addWork(Work* work)
      {
36 37
         std::lock_guard<Mutex> mutexLock(mutex);

38 39 40 41
         newWorkCond.signal();

         workList.push_back(work);
      }
42

43 44
   private:
      WorkList workList;
45

46 47 48 49 50
      Mutex mutex;
      Condition newWorkCond;
};

#endif /*WORKQUEUE_H_*/