Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 线程构建块:死锁,因为所有线程都已用完_C++_Multithreading_C++11_Deadlock_Tbb - Fatal编程技术网

C++ 线程构建块:死锁,因为所有线程都已用完

C++ 线程构建块:死锁,因为所有线程都已用完,c++,multithreading,c++11,deadlock,tbb,C++,Multithreading,C++11,Deadlock,Tbb,在“英特尔线程构建块”框架中,如何确保所有线程不忙于等待其他线程完成 例如,考虑以下代码 #include <tbb/tbb.h> #include <vector> #include <cstdlib> #include <future> #include <iostream> std::future<bool> run_something(std::function<bool(bool)> func, bo

在“英特尔线程构建块”框架中,如何确保所有线程不忙于等待其他线程完成

例如,考虑以下代码

#include <tbb/tbb.h>
#include <vector>
#include <cstdlib>
#include <future>
#include <iostream>

std::future<bool> run_something(std::function<bool(bool)> func, bool b) {
  auto task = std::make_shared<std::packaged_task<bool()> >(std::bind(func, b));
  std::future<bool> res = task->get_future();
  tbb::task_group g;
  g.run([task]() { (*task)(); });
  return res;
};

int main() {
  tbb::parallel_for(0, 100, 1, [=](size_t i) {
    g.run([] () {
      std::cout << "A" << std::endl;  
      run_something([] (bool b) { return b; }, true).get();
    });
  });
  return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
std::未来运行(std::函数func,布尔b){
自动任务=std::使_共享(std::bind(func,b));
std::future res=task->get_future();
任务组g;
g、 运行([task](){(*task)(;});
返回res;
};
int main(){
tbb::用于(0,100,1,[=])(大小为i)的并行{
g、 运行([])(){

std::coutstd::future
与TBB可选的并行范例不兼容。
std::future::get()
应该真正命名为
let\u me\u block\u in\u system\u wait\u here()
。除非您希望TBB死锁,否则禁止在TBB任务计划程序未知的TBB任务之间实现任何类型的同步。也就是说,使用TBB方式表示TBB任务之间的依赖关系

可选的并行性意味着您的代码必须仅使用一个线程才能正确运行。只有
tbb::task::enqueue()
承诺除了主线程之外,还至少有一个工作线程

您的代码甚至不应该编译,因为您在
main()
中使用
g.run()
,而没有声明
g
。并且禁止在调用
wait()之前销毁
task\u group
如中为析构函数所述:
要求:在销毁任务组之前必须调用方法wait,否则析构函数将引发异常。


至于共享线程池。是的,TBB有一个共享线程池。但您可以使用
task\u arena
控制共享工作的方式。没有任务可以离开arena,但工作线程可以在运行任务之间的时间内跨arenas迁移。

不要在同一线程池上运行相互依赖的内容。为每个任务分配group它自己的池。但是我该怎么做呢。看起来TBB默认共享线程池。