簡體   English   中英

提升thread_group將unique_ptr的所有權移至線程

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM