是否有类似于PPL在TBB中的任务延续?
我知道manuall分配tbb::task
并手动分配延续任务的低级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中实现这一目标?
答案 0 :(得分:4)
是的,您可以在http://www.threadingbuildingblocks.org/docs/help/reference/task_scheduler/catalog_of_recommended_task_patterns.htm阅读几种推荐的TBB延续样式。但是,通过设计TBB库,它们都不像您的PPL示例那样使用C ++ 11构造。
如果您的问题确实是“TBB是否有用于任务延续的C ++ 11界面”,则答案是“否”。
答案 1 :(得分:1)
没有任何直接的内容,我在博客here上发布了一个如何使用task_group(在tbb中)执行此操作的示例。
语法类似但不是100%相同,因为它是在任务存在之前发布的。
void SimpleContinuation()
{
auto task1 = run_task([](){ContinueableTask(1);});
//task 2 depends on task 1
auto task2 = run_when(task1, [](){ContinueableTask(2);});
wait_for_all(task1, task2);
}