有没有类似 PPL 在 TBB 中的任务延续?tbb::task
我知道手动分配s 和手动分配延续任务并为它们手动管理引用计数的低级 TBB 方法:
struct FibContinuation: public task {
long* const sum;
long x, y;
FibContinuation( long* sum_ ) : sum(sum_) {}
task* execute() {
*sum = x+y;
return NULL;
}
};
struct FibTask: public task {
const long n;
long* const sum;
FibTask( long n_, long* sum_ ) :
n(n_), sum(sum_)
{}
task* execute() {
if( n<CutOff ) {
*sum = SerialFib(n);
return NULL;
} else {
// long x, y; This line removed
FibContinuation& c =
*new( allocate_continuation() ) FibContinuation(sum);
FibTask& a = *new( c.allocate_child() ) FibTask(n-2,&c.x);
FibTask& b = *new( c.allocate_child() ) FibTask(n-1,&c.y);
// Set ref_count to "two children plus one for the wait".
c.set_ref_count(2);
spawn( b );
spawn( a );
// *sum = x+y; This line removed
return NULL;
}
}
};
这简直太可怕了。您必须提前知道将产生多少子任务,并适当地手动设置引用计数。这是非常脆弱的编码...
PPL 指定延续的方式非常简单:
create_task([]()->bool
{
// compute something then return a bool result
return true
}).then([](bool aComputedResult)
{
// do something with aComputedResult
});
您如何在 TBB 中实现这一目标?