下面的代码编译完成后,似乎陷入了我与英特尔TBB所做任务中的某处。它只是运行并不显示任何内容,我必须杀死程序才能结束它。基本上,我是在书中的一个例子之后对此进行了模拟,而且我可能做得不正确。我在做这些任务时做错了什么?我正在使用g ++ 4.8.4,并认为我正在使用TBB 3.9。如何获得与TBB一起工作的任务
/*
g++ test0.cpp -o test0.out -std=c++11 -ltbb
*/
#include <iostream>
#include "tbb/task_scheduler_init.h"
#include "tbb/task.h"
using namespace tbb;
long serial_fibo(long n) {
if(n < 2) {
return n;
} else {
return serial_fibo(n - 1) + serial_fibo(n - 2);
}
}
class Fibo_Task: public task {
public:
const long n;
long* const sum;
Fibo_Task(long _n_, long* _sum_) :
n(_n_), sum(_sum_) {}
// override virtual function task::execute
task *execute() {
if(n < 4) {
*sum = serial_fibo(n);
} else {
long x = 0, y = 0;
// references x
Fibo_Task& a =
*new(task::allocate_root())
Fibo_Task(n - 1, &x);
// references y
Fibo_Task& b =
*new(task::allocate_root())
Fibo_Task(n - 2, &y);
// two children and another to wait
set_ref_count(3);
spawn(a);
spawn_and_wait_for_all(b);
*sum = x + y;
}
return NULL;
}
};
long parallel_fibo(long n) {
long sum;
Fibo_Task& a =
*new(task::allocate_root())
Fibo_Task(n, &sum);
task::spawn_root_and_wait(a);
return sum;
}
int main() {
task_scheduler_init init;
long number = 8;
long first = serial_fibo(number);
long second = parallel_fibo(number);
std::cout << "first: " << first << "\n";
std::cout << "second: " << second << "\n";
return 0;
}
您是否在调试器中暂停了一下以查看执行卡住的位置? – Cameron
在伪递归代码中对'spawn_and_wait_for_all'的调用看起来很腥......你不只是想等待'a'和'b'而不是你自己的任务? – Cameron