Devel Lib
ThreadPool.h
1 #pragma once
2 
3 #include <thread>
4 #include <functional>
5 #include "Threading/SafeQueue/SafeQueue.h"
6 
9 namespace Devel::Threading {
10  typedef std::function<void()> ThreadPoolTaskFn;
11 
16  class CThreadPool {
17  public:
20  enum EError {
23  };
24 
25  public:
28  : m_nWorkerCount(3), m_fIsExecuted(false) {
29  }
30 
33  explicit CThreadPool(const size_t i_nWorkerCount)
34  : CThreadPool() {
35  this->setWorkerCount(i_nWorkerCount);
36  }
37 
38  // @brief Destructor for CThreadPool.
39  ~CThreadPool() {
40  this->stop();
41  }
42 
43 
47  CThreadPool(CThreadPool &i_oPool) = delete;
48 
52  CThreadPool(CThreadPool *i_pPool) = delete;
53 
54  public:
57  EError execute();
58 
62  EError execute(const size_t i_nWorkerCount) {
63  this->setWorkerCount(i_nWorkerCount);
64  return this->execute();
65  }
66 
69  void stop(bool i_fClearTasks = true);
70 
71  private:
73  void handleWorker();
74 
75  public:
78  inline void addTask(const ThreadPoolTaskFn &i_oTask) { this->m_aoTasks.enqueue(i_oTask); }
79 
82  inline void addTask(ThreadPoolTaskFn &&i_oTask) { this->m_aoTasks.enqueue(std::move(i_oTask)); }
83 
86  inline void setWorkerCount(const size_t i_nWorkerCount) {
87  this->m_nWorkerCount = (i_nWorkerCount == 0 ? 1 : i_nWorkerCount);
88  }
89 
90  public:
93  size_t workerCount() const { return this->m_nWorkerCount; }
94 
97  bool isExecuted() const { return this->m_fIsExecuted; }
98 
99  private:
102  size_t m_nWorkerCount;
103 
106  std::vector<std::thread> m_aoWorker;
107 
111 
114  bool m_fIsExecuted;
115  };
116 }
void enqueue(const T &i_tValue)
Enqueues an element into the safe queue.
Definition: SafeQueue.h:127
A class that implements a thread pool for executing tasks concurrently.
Definition: ThreadPool.h:16
CThreadPool(CThreadPool &i_oPool)=delete
Deleted copy constructor for CThreadPool. This copy constructor is explicitly deleted to prevent copy...
EError execute(const size_t i_nWorkerCount)
Executes the thread pool with a specified worker count.
Definition: ThreadPool.h:62
EError
An enumeration of possible errors that can occur when executing the thread pool.
Definition: ThreadPool.h:20
@ EAlreadyExecuted
The thread pool has already been executed.
Definition: ThreadPool.h:22
@ ESuccess
The operation was successful.
Definition: ThreadPool.h:21
CThreadPool()
Default constructor for CThreadPool.
Definition: ThreadPool.h:27
void stop(bool i_fClearTasks=true)
Stops the execution of the thread pool.
Definition: ThreadPool.cpp:20
size_t workerCount() const
Returns the current worker count of the thread pool.
Definition: ThreadPool.h:93
CThreadPool(const size_t i_nWorkerCount)
Constructor that sets the worker count for the thread pool.
Definition: ThreadPool.h:33
EError execute()
Executes the thread pool with the current worker count.
Definition: ThreadPool.cpp:5
bool isExecuted() const
Checks if the thread pool has been executed.
Definition: ThreadPool.h:97
void addTask(const ThreadPoolTaskFn &i_oTask)
Adds a task to the task queue.
Definition: ThreadPool.h:78
void setWorkerCount(const size_t i_nWorkerCount)
Sets the worker count for the thread pool.
Definition: ThreadPool.h:86
void addTask(ThreadPoolTaskFn &&i_oTask)
Adds a task to the task queue.
Definition: ThreadPool.h:82
CThreadPool(CThreadPool *i_pPool)=delete
Deleted constructor for CThreadPool that takes a pointer. This constructor is explicitly deleted to p...
The namespace encapsulating threading related classes and functions in the Devel framework.