下面的代碼編譯完成後,似乎陷入了我與英特爾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