TaskRunner.h
1 /* Distributed under the Apache License, Version 2.0.
2  See accompanying NOTICE file for details.*/
3 
4 #pragma once
5 
6 #include "TaskQueue.h"
7 
8 #include <queue>
9 #include <memory>
10 #include <mutex>
11 
12 //--------------------------------------------------------------------------------------------------
15 //--------------------------------------------------------------------------------------------------
16 class CDM_DECL TaskRunner : public TaskQueue
17 {
18 public:
19  TaskRunner();
20  TaskRunner(unsigned int threadCount);
21  virtual ~TaskRunner() = default;
22 
23  TaskRunner(const TaskRunner& other) = delete;
24  TaskRunner& operator=(const TaskRunner& other) = delete;
25 
26  void AddTask(std::unique_ptr<Task> task);
27  void Run();
28 
29  virtual std::unique_ptr<Task> GetNextTask() override;
30 
31  void EchoProgress(bool echo) { m_echoPercentComplete = echo; }
32  virtual void ReportTaskComplete() override;
33 
34 protected:
35  void PrintProgress();
36 
37  mutable std::recursive_mutex m_taskQueueMutex;
38  std::queue<std::unique_ptr<Task>> m_tasks;
39 
40  unsigned int m_threadCount = 1;
41 
42  bool m_echoPercentComplete = false;
43  size_t m_totalTaskCount = 0;
44  unsigned int m_tasksComplete = 0;
45 };
virtual void ReportTaskComplete()=0
std::recursive_mutex m_taskQueueMutex
Mutex used to synchronize concurrent access to the task queue.
Definition: TaskRunner.h:37
std::queue< std::unique_ptr< Task > > m_tasks
Queue of tasks to be run.
Definition: TaskRunner.h:38
void EchoProgress(bool echo)
Definition: TaskRunner.h:31
This class handles concurrent execution of tasks.
Definition: TaskRunner.h:16
Interface through which TaskProcessors retrieve Tasks from the TaskRunner.
Definition: TaskQueue.h:13
virtual std::unique_ptr< Task > GetNextTask()=0