问题
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