From 01517f34210857a7e0c55b95a57ebf683918951b Mon Sep 17 00:00:00 2001 From: Ian Roddis Date: Wed, 16 Jun 2021 10:51:38 -0300 Subject: [PATCH] adding futures --- daggy/src/ThreadPool.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/daggy/src/ThreadPool.cpp b/daggy/src/ThreadPool.cpp index 3e4963a..99d951a 100644 --- a/daggy/src/ThreadPool.cpp +++ b/daggy/src/ThreadPool.cpp @@ -3,6 +3,8 @@ using namespace daggy; ThreadPool::ThreadPool(size_t nWorkers) { + shutdown_ = false; + std::lock_guard lk(guard_); for (size_t i = 0; i < nWorkers; ++i) { workers_.emplace_back([&]() { while (true) { @@ -36,9 +38,11 @@ void ThreadPool::shutdown() { } std::future ThreadPool::addTask(std::function fn) { + std::packaged_task task(fn); + std::future result = task.get_future(); { std::unique_lock lk(guard_); - taskQueue_.push_back(fn); + taskQueue_.push_back(std::move(task)); } cv_.notify_one(); }