提高的unique_ptr的thread_group举动所有权线程 [英] boost thread_group move ownership of unique_ptr to thread
本文介绍了提高的unique_ptr的thread_group举动所有权线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么解决方法,使这一code运行?在code结果试图引用删除功能。 的unique_ptr
被分配在一个循环,然后传递到线程,后来摆脱了。
的boost :: thread_group线程;
的std ::的unique_ptr< ScenarioResult> scenario_result;而((scenario_result = scenarioStock.getNextScenario())!= nullptr)
{
threads.create_thread(升压::绑定(安培;模拟:: RunSimulation,升压:: REF(grid_sim)的std ::移动(scenario_result)));
}
解决方案
您可以使用lambda前pression而不是的boost ::绑定
threads.create_thread([安培] {grid_sim.RunSimulation(性病::移动(scenario_result));});
与你在做什么的问题是, create_thread
试图复制仿函数来说,绑定
创建和失败,因为的unique_ptr
绑定参数的presence使仿函数布展只。
What workaround exists to make this code run? The code results in "Attempting to reference a deleted function". unique_ptr
is assigned in a loop and then passed on to thread and later got rid of.
boost::thread_group threads;
std::unique_ptr<ScenarioResult> scenario_result;
while ((scenario_result = scenarioStock.getNextScenario()) != nullptr)
{
threads.create_thread(boost::bind(&Simulation::RunSimulation, boost::ref(grid_sim), std::move(scenario_result)));
}
解决方案
You can use a lambda expression instead of boost::bind
threads.create_thread([&] { grid_sim.RunSimulation(std::move(scenario_result)); });
The problem with what you're doing is that create_thread
attempts to copy the functor that bind
creates, and that fails because the presence of the unique_ptr
bound argument makes the functor move-only.
这篇关于提高的unique_ptr的thread_group举动所有权线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文