【问题标题】:Can't create thread in thread_group with a packaged_task无法使用 packaged_task 在 thread_group 中创建线程
【发布时间】:2012-06-05 21:19:25
【问题描述】:

我想创建一组线程,它们都有未来(这就是我使用 packaged_tasks 的原因),所以我可以使用它们返回的值。 但是下面的代码在编译过程中会返回错误:

boost::thread_group group;


for(int i=0; i<4; i++){
    boost::packaged_task<int> task(std::bind(&matchThis,someStr, anotherStr));
    boost::unique_future<int> fi=task.get_future();

    //add task to task-group
    group.create_thread(boost::move(task));
    group.join_all();
}

这是错误:

c:\boost\boost\thread\detail\thread_group.hpp:42: 错误: “boost::packaged_task::packaged_task”:无法访问在 boost::packaged_task-class 中声明的私有成员 .和 [ R=整数] ...

当我创建这样的线程时,这段代码运行良好:

boost::thread thread(boost::move(task));

那么改用 thread_groups 有什么问题呢?

【问题讨论】:

    标签: c++ multithreading boost


    【解决方案1】:

    我假设您没有使用 C++11(因为我认为如果您使用它会起作用。)

    boost::thread 已“启用移动”,因此它可以接受boost::move 返回的模拟右值引用。 boost::thread_group 没有以同样的方式“启用移动”,因此 create_thread 的参数必须是可复制的,而 packaged_task 是不可复制的。

    我建议检查 Boost Trac 错误数据库,如果还没有添加对 create_thread 的请求以支持移动的右值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 2012-06-10
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      相关资源
      最近更新 更多