-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
I've simplified the enqueue method, now the additional classes are not needed and the container is std::deque<std::function<void()>>:
template<class Result, class Callable>
std::future<Result> ThreadPoolManager::enqueue(Callable callable) {
/**
* Create task, we use RAII here and copy-capture lambda later as this std::packed_task has to be
* deleted after it was executed.
*/
auto task = std::make_shared<std::packaged_task<Result()>>(callable);
/** Get the result of the task */
std::future<Result> result = task->get_future();
{
/** Lock the queue */
std::unique_lock<Mutex> lock(mutex_);
/** Push new task to the queue */
tasks_.push_back([=] {
/** Execute task */
(*task)();
});
}
/** Notify one worker that there is a work to do */
conditional_variable_.notify_one();
return result;
}Reactions are currently unavailable