TBB中有类似PPL的任务延续的东西吗?我知道手动分配tbb::tasks 和手动分配连续任务以及手动管理它们的引用计数的低级 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;
}
}
};
Run Code Online (Sandbox Code Playgroud)
这简直太可怕了。您必须提前知道将生成多少个子任务,并适当地手动设置引用计数。这是非常脆弱的编码......
PPL 指定延续的方式非常简单:
create_task([]()->bool
{
// compute something then return a bool result
return true
}).then([](bool aComputedResult)
{
// do something with aComputedResult
});
Run Code Online (Sandbox Code Playgroud)
您如何在 TBB 中实现这一目标?
是的,您可以在http://www.threadingbuildingblocks.org/docs/help/reference/task_scheduler/catalog_of_recommended_task_patterns.htm中阅读到几种推荐的 TBB 延续样式。然而,根据 TBB 库的设计,它们都不像您的 PPL 示例那样使用 C++11 结构。
如果您的问题确实是“TBB 是否有用于任务延续的 C++11 接口”,那么答案是“否”。
| 归档时间: |
|
| 查看次数: |
1780 次 |
| 最近记录: |