[英]boost thread_group move ownership of unique_ptr to thread
有什么解決方法可以使此代碼運行? 該代碼導致“嘗試引用已刪除的功能”。 在循環中分配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
綁定參數的存在使函子只能移動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.