yohhoyの日記

技術的メモをしていきたい日記

tbb::flow::sequencer_nodeとsequence order numberの落とし穴(続き)

前記事ではフローグラフによる実装を行ったが、この設計ならばで並列パイプラインアルゴリズム parallel_pipeline の方が簡潔に記述できる。というより、この手の線形パイプライン処理はパイプライン(pipeline)アルゴリズムで実装すべき。

#include <iostream>
#include "tbb/pipeline.h"
using namespace tbb;

int n = 0;

// 入力フィルタ[serial_in_order]: 数列{0,1,..9}を生成する
auto src_filter = make_filter<void, double>(
  filter::serial_in_order,
  [&n](flow_control& fc) {
    if (n >= 10)
      fc.stop();
    return n++;
  });
// 処理フィルタ[parallel]: 値を2倍する
auto proc_filter = make_filter<double, double>(
  filter::parallel,
  [](double x) {
    return x * 2.0;
  });
// 出力フィルタ[serial_in_order]: 標準出力へダンプ
auto dump_filter = make_filter<double, void>(
  filter::serial_in_order,
  [](double x) {
    std::cout << x << std::endl;
  });

const std::size_t token = 5;  // 最大トークン数
parallel_pipeline(token, src_filter & proc_filter & dump_filter);