boost thread_group move ownership of unique_ptr to thread

混江龙づ霸主 提交于 2019-12-31 07:06:46

问题


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)));
}

回答1:


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.



来源:https://stackoverflow.com/questions/35591510/boost-thread-group-move-ownership-of-unique-ptr-to-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!