c++ - boost thread_group 将 unique_ptr 的所有权移动到线程

标签 c++ multithreading c++11 boost unique-ptr

有什么变通方法可以使此代码运行?该代码导致“试图引用已删除的函数”。 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/

相关文章:

android - 所有线程完成后如何正确停止服务

java : stop safety a thread; not using while and boolean flag

c++ - 如何以编程方式确定表达式在 C++ 中是右值还是左值?

c++ - 没有 O(1) 操作来连接来自两个 forward_lists 的元素?

c++ - 命令行错误中缺少DSO(动态共享库)的解决方案是什么?

c++ - 超出范围时不会删除堆栈上的局部变量

c++ - opengl 3d纹理问题

C++ <策略文件请求/>

c# - 如何在 C# 中创建基于 DataTable.Rows.Count 的对象?

python - 正则表达式速度 : Python x6 times faster than C++11 under VS2013?