4

有没有类似 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 中实现这一目标?

4

2 回答 2

5

是的,您可以在http://www.threadingbuildingblocks.org/docs/help/reference/task_scheduler/catalog_of_recommended_task_patterns.htm阅读到几种推荐的 TBB 延续样式。但是,根据 TBB 库的设计,它们都没有像您的 PPL 示例那样使用 C++11 结构。

如果您的问题真的是“TBB 是否有用于任务继续的 C++11 接口”,那么答案是“否”。

于 2013-11-23T15:29:43.947 回答
2

没有任何直接的东西,我很久以前在我的博客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);
}
于 2013-04-21T16:18:51.383 回答