boost thread_group 将 unique_ptr 的所有权移至线程

boost thread_group move ownership of unique_ptr to thread

有什么解决方法可以生成此代码 运行?代码结果为 "Attempting to reference a deleted function"。 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 绑定参数的存在使得函子只移动。