summaryrefslogtreecommitdiff
path: root/lib/worker.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-19 20:35:29 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-01-19 20:35:29 +0000
commit96a58cc251354954241aa2c9008b25d98daaad92 (patch)
tree383d379fe8bbf5026a5a0cdea4a376485c65b318 /lib/worker.h
parentFactor to support worlds, objects, windows etc (diff)
downloadilt-96a58cc251354954241aa2c9008b25d98daaad92.tar.bz2
ilt-96a58cc251354954241aa2c9008b25d98daaad92.tar.xz
ilt-96a58cc251354954241aa2c9008b25d98daaad92.zip
Add basic work and worker thread pool
Well... that requires GCC 11 cos 10 doesn't implement semaphore.
Diffstat (limited to 'lib/worker.h')
-rw-r--r--lib/worker.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/worker.h b/lib/worker.h
new file mode 100644
index 0000000..5a13138
--- /dev/null
+++ b/lib/worker.h
@@ -0,0 +1,46 @@
+#ifndef WORKER_H
+#define WORKER_H
+
+#include <deque>
+#include <memory>
+#include <mutex>
+#include <semaphore>
+#include <special_members.hpp>
+#include <thread>
+#include <utility>
+#include <vector>
+
+class Work;
+
+class Worker {
+public:
+ Worker();
+ ~Worker();
+
+ NO_COPY(Worker);
+ NO_MOVE(Worker);
+
+ using WorkPtr = std::unique_ptr<Work>;
+
+ template<typename T, typename... Params>
+ void
+ addWork(Params &&... params)
+ {
+ addWork(std::make_unique<T>(std::forward<Params>(params)...));
+ }
+
+ void addWork(WorkPtr w);
+
+private:
+ void worker();
+
+ using Threads = std::vector<std::thread>;
+ using ToDo = std::deque<WorkPtr>;
+
+ Threads threads;
+ ToDo todo;
+ std::counting_semaphore<16> todoLen;
+ std::mutex todoMutex;
+};
+
+#endif