提交 588b59cf 编写于 作者: J Jakob Progsch

Check stop condition under lock. Use emplace instead of push.

上级 20061c5c
...@@ -60,10 +60,6 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) ...@@ -60,10 +60,6 @@ auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type> -> std::future<typename std::result_of<F(Args...)>::type>
{ {
typedef typename std::result_of<F(Args...)>::type return_type; typedef typename std::result_of<F(Args...)>::type return_type;
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
auto task = std::make_shared< std::packaged_task<return_type()> >( auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...) std::bind(std::forward<F>(f), std::forward<Args>(args)...)
...@@ -72,7 +68,12 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) ...@@ -72,7 +68,12 @@ auto ThreadPool::enqueue(F&& f, Args&&... args)
std::future<return_type> res = task->get_future(); std::future<return_type> res = task->get_future();
{ {
std::unique_lock<std::mutex> lock(queue_mutex); std::unique_lock<std::mutex> lock(queue_mutex);
tasks.push([task](){ (*task)(); });
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
} }
condition.notify_one(); condition.notify_one();
return res; return res;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册