NAP
threading.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "readerwriterqueue.h"
9 #include "blockingconcurrentqueue.h"
10 
11 // External Includes
12 #include <functional>
13 #include <thread>
14 #include <vector>
15 
16 namespace nap
17 {
22  class TaskQueue final
23  {
24  public:
25  // a task in the queue is a function object
26  using Task = std::function<void()>;
27 
28  public:
32  TaskQueue(int maxQueueItems = 20);
36  bool enqueue(Task task) { return mQueue.enqueue(task); }
37 
42  void processBlocking();
43 
47  void process();
48 
49  private:
50  moodycamel::BlockingReaderWriterQueue<Task> mQueue;
51  std::vector<Task> mDequeuedTasks;
52  };
53 
54 
59  {
60  public:
64  WorkerThread();
65 
70  WorkerThread(bool blocking, int maxQueueItems = 20);
71  virtual ~WorkerThread();
72 
76  void enqueue(TaskQueue::Task task) { mTaskQueue.enqueue(task); }
77 
81  void start();
82 
86  void stop();
87 
91  bool isRunning() { return mRunning; }
92 
96  virtual void loop() { }
97 
98  private:
99  std::unique_ptr<std::thread> mThread = nullptr;
100  std::atomic<bool> mRunning;
101  bool mBlocking = true;
102  TaskQueue mTaskQueue;
103  };
104 
105 
109  class ThreadPool final
110  {
111  public:
112  ThreadPool(int numberOfThreads = 1, int maxQueueItems = 20, bool realTimePriority = false);
113  ~ThreadPool();
114 
119  assert(task != nullptr);
120  mTaskQueue.enqueue(task);
121  }
122 
126  void shutDown();
127 
131  void resize(int numberOfThreads);
132 
136  int getThreadCount() const { return int(mThreads.size()); }
137 
146  int getTaskCount() const { return mTaskQueue.size_approx(); }
147 
151  bool isStopping() const { return (mStop == true); }
152 
153  private:
154  void addThread();
155 
156  std::vector<std::thread> mThreads;
157  std::atomic<bool> mStop;
158  moodycamel::BlockingConcurrentQueue<TaskQueue::Task> mTaskQueue;
159  bool mRealTimePriority = false;
160  };
161 }
nap::TaskQueue::TaskQueue
TaskQueue(int maxQueueItems=20)
nap::WorkerThread
Definition: threading.h:58
nap::TaskQueue::enqueue
bool enqueue(Task task)
Definition: threading.h:36
nap::WorkerThread::isRunning
bool isRunning()
Definition: threading.h:91
nap::ThreadPool::~ThreadPool
~ThreadPool()
nap::ThreadPool::execute
void execute(TaskQueue::Task task)
Definition: threading.h:118
nap::WorkerThread::stop
void stop()
nap::WorkerThread::~WorkerThread
virtual ~WorkerThread()
nap::WorkerThread::start
void start()
nap::TaskQueue::process
void process()
nap::TaskQueue::processBlocking
void processBlocking()
nap::TaskQueue
Definition: threading.h:22
nap::ThreadPool::ThreadPool
ThreadPool(int numberOfThreads=1, int maxQueueItems=20, bool realTimePriority=false)
nap::ThreadPool::getTaskCount
int getTaskCount() const
Definition: threading.h:146
nap::TaskQueue::Task
std::function< void()> Task
Definition: threading.h:26
nap::ThreadPool::getThreadCount
int getThreadCount() const
Definition: threading.h:136
nap::WorkerThread::loop
virtual void loop()
Definition: threading.h:96
nap::ThreadPool::resize
void resize(int numberOfThreads)
nap::ThreadPool::shutDown
void shutDown()
nap
Definition: templateapp.h:17
nap::WorkerThread::enqueue
void enqueue(TaskQueue::Task task)
Definition: threading.h:76
nap::ThreadPool::isStopping
bool isStopping() const
Definition: threading.h:151
nap::WorkerThread::WorkerThread
WorkerThread()
nap::ThreadPool
Definition: threading.h:109