[英]threadpool c++ implementation questions
我的問題是關於將任務添加到線程池的功能,這些是分別在上面的項目中添加和排隊 。
因為這些看起來很相似我在這里張貼了一塊(來自第二個項目)
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// 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();
return res;
}
容器任務聲明為:
std::queue< std::function<void()> > tasks;
所以我的問題是:
如果你還在尋找答案(你大多是自己回答):
當前的HPC線程池實現主要使用工作竊取調度程序:每個工作程序都有自己的隊列。 工作人員從他/她自己的隊列中拉出並推送工作,直到他完成了自己隊列中的所有任務。 然后它從其他工人竊取任務並執行這些任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.