有什么变通方法可以使此代码运行?该代码导致“试图引用已删除的函数”。 unique_ptr
在一个循环中被分配,然后传递给线程,然后被删除。
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)));
}
最佳答案
您可以使用 lambda 表达式代替 boost::bind
threads.create_thread([&] { grid_sim.RunSimulation(std::move(scenario_result)); });
您正在做的事情的问题是 create_thread
试图复制 bind
创建的仿函数,但由于 unique_ptr< 的存在而失败
绑定(bind)参数使仿函数只能移动。
关于c++ - boost thread_group 将 unique_ptr 的所有权移动到线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591510/