DIE Engine
Loading...
Searching...
No Matches
workerpool.h
Go to the documentation of this file.
1
11
12#ifndef WORKERPOOL_H
13#define WORKERPOOL_H
14
15#include <vector>
16#include <thread>
17#include <functional>
18#include <mutex>
19#include <condition_variable>
20
21/*****************************************************************************/
26{
27public:
28 static constexpr unsigned WorkersMax = 8;
29
30 WorkerPool();
32
34 void enqueue(std::function<void()> job);
35
37 void waitAll();
38
40 int workerCount() const {return activeWorkers;}
41
42private:
43 std::vector<std::thread> workers;
44 std::vector<std::function<void()>> jobs;
45
46 std::mutex queueMutex;
47
48 std::condition_variable jobCV;
49 std::condition_variable waitCV;
50
51 bool stop;
52 int jobsPrepared;
53 int jobsUnfinished;
54 int activeWorkers;
55};
56
57#endif // WORKERPOOL_H
void enqueue(std::function< void()> job)
Add a job to the pool (jobs may run in any order).
Definition workerpool.cpp:62
WorkerPool()
Definition workerpool.cpp:17
static constexpr unsigned WorkersMax
Definition workerpool.h:28
int workerCount() const
Number of worker threads in the pool.
Definition workerpool.h:40
void waitAll()
Block until all enqueued jobs have completed.
Definition workerpool.cpp:73
~WorkerPool()
Definition workerpool.cpp:51