4

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;
        }
    }
};

それは単に恐ろしいことです。生成する子タスクの数を事前に把握し、参照カウントを適切に手動で設定する必要があります。これは非常に壊れやすいコーディングです...

継続を指定する 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

直接的なものは何もありません。私のブログのこちらに、何年も前に 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 に答える