libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
threadpool.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <thread>
11#include <functional>
12#include <type_traits>
14#include <promeki/core/error.h>
15#include <promeki/core/mutex.h>
17#include <promeki/core/future.h>
18#include <promeki/core/list.h>
19
21
45 public:
53
58
59 ThreadPool(const ThreadPool &) = delete;
60 ThreadPool &operator=(const ThreadPool &) = delete;
61 ThreadPool(ThreadPool &&) = delete;
62 ThreadPool &operator=(ThreadPool &&) = delete;
63
74 template <typename F>
76 using R = std::invoke_result_t<F>;
77 auto task = std::make_shared<std::packaged_task<R()>>(
78 std::forward<F>(callable));
79 Future<R> fut(task->get_future());
80 bool runInline = false;
81 {
82 Mutex::Locker locker(_mutex);
83 if(_threadCount == 0) {
84 runInline = true;
85 } else {
86 _tasks.pushToBack([task]() { (*task)(); });
87 _cv.wakeOne();
88 }
89 }
90 if(runInline) (*task)();
91 return fut;
92 }
93
103 void setThreadCount(int count);
104
109 int threadCount() const;
110
115 int activeThreadCount() const;
116
121
128
132 void clear();
133
134 private:
135 using Task = std::function<void()>;
136
137 void workerFunc();
138 void spawnThreads(int count);
139
140 mutable Mutex _mutex;
141 WaitCondition _cv;
142 WaitCondition _doneCv;
143 List<Task> _tasks;
144 List<std::thread> _threads;
145 int _threadCount = 0;
146 int _activeCount = 0;
147 bool _shutdown = false;
148};
149
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Dynamic array container wrapping std::vector.
Definition list.h:40
void pushToBack(const T &value)
Pushes an item onto the back of the list.
Definition list.h:455
RAII scoped locker for Mutex.
Definition mutex.h:41
Mutual exclusion lock wrapping std::mutex.
Definition mutex.h:33
General-purpose thread pool for submitting callable tasks.
Definition threadpool.h:44
int activeThreadCount() const
Returns the number of threads currently executing tasks.
int threadCount() const
Returns the current thread count.
Error waitForDone(unsigned int timeoutMs)
Blocks until all submitted tasks have completed or the timeout expires.
~ThreadPool()
Destructor. Signals shutdown and joins all worker threads.
void clear()
Removes all pending (not yet running) tasks from the queue.
void setThreadCount(int count)
Resizes the thread pool.
auto submit(F &&callable) -> Future< std::invoke_result_t< F > >
Submits a callable for asynchronous execution.
Definition threadpool.h:75
void waitForDone()
Blocks until all submitted tasks have completed.
ThreadPool(int threadCount=-1)
Constructs a ThreadPool with the given number of threads.
Condition variable wrapping std::condition_variable.
Definition waitcondition.h:26
void wakeOne()
Wakes one waiting thread.
Definition waitcondition.h:94
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19