64 : nThreads(n), terminate(false), working(0)
67 for (
unsigned i = 0; i < n; i++)
69 threads[i] = std::thread(&ThreadPool::main,
this);
73 void queueJob(
const std::function<
void(
void)> & job)
76 std::unique_lock<std::mutex> lock(queueLock);
77 jobs.emplace(std::move(job));
81 queueCondition.notify_one();
88 std::unique_lock<std::mutex> lock(queueLock);
106 std::unique_lock<std::mutex> lock(queueLock);
109 queueCondition.notify_all();
110 for (std::thread & t : threads)
132 for (
unsigned i = 0; i < n-1; i++)
134 threads[i] = std::thread(&ThreadPool::main,
this);
155 for (
unsigned i = 0; i < n+1; i++)
157 threads[i] = std::thread(&ThreadPool::main,
this);
162 size_t size(){
return threads.size();}
170 std::function<void(
void)> job;
172 std::unique_lock<std::mutex> lock(queueLock);
175 lock, [
this] {
return !jobs.empty() || terminate;}
183 job = std::move(jobs.front());
190 std::unique_lock<std::mutex> lock(queueLock);
197 const size_t nThreads;
201 std::queue<std::function<void(
void)>> jobs;
202 std::vector<std::thread> threads;
206 std::mutex queueLock;
207 std::condition_variable queueCondition;