无法使用packaged_task在thread_group中创建线程

时间:2012-06-05 21:19:26

标签: c++ multithreading boost

我想创建一组线程,这些线程都有未来(这就是我使用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 = INT]   ...

当我创建这样的线程时,这段代码非常有效:

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

那么使用thread_groups会出现什么问题呢?

1 个答案:

答案 0 :(得分:1)

我假设你没有使用C ++ 11(因为我认为如果你这样做会有用。)

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

我建议检查Boost Trac错误数据库,如果没有人已经添加create_thread请求以支持移动的rvalues。