我想创建一组线程,这些线程都有未来(这就是我使用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会出现什么问题呢?
答案 0 :(得分:1)
我假设你没有使用C ++ 11(因为我认为如果你这样做会有用。)
boost::thread
已“启用移动”,因此它可以接受boost::move
返回的模拟右值引用。 boost::thread_group
没有以相同的方式“启用移动”,因此create_thread
的参数必须是可复制的,并且packaged_task
不可复制。
我建议检查Boost Trac错误数据库,如果没有人已经添加create_thread
请求以支持移动的rvalues。