正如您所推测的那样, continue_node 不会触发主体,直到它收到与前任数量相等的 continue_msgs 数量。如果您在构造函数中指定前驱计数,则该计数将初始化为该数字,并向节点添加一条边会增加该计数。
我包括一个小程序来证明这一点:
#include "tbb/flow_graph.h"
#include <iostream>
using namespace tbb::flow;
struct continue_body {
continue_msg operator()(const continue_msg& /*c*/) {
return continue_msg();
}
};
struct output_body {
int my_count;
output_body() { my_count = 0; }
continue_msg operator()(const continue_msg&) {
std::cout << "Received continue_msg " << ++my_count << "\n";
}
};
int
main() {
graph g;
continue_node<continue_msg> counting_node(g, continue_body());
continue_node<continue_msg> counting_node2(g, 1, continue_body());
function_node<continue_msg> output_node(g, serial, output_body());
make_edge(counting_node, counting_node2);
make_edge(counting_node2, output_node);
for(int i = 0; i < 10; ++i) {
counting_node.try_put(continue_msg());
}
g.wait_for_all();
}
我将 continue_nodecounting_node2 初始化为 1,并为其添加了一条边。输出应该是
Received continue_msg 1
Received continue_msg 2
Received continue_msg 3
Received continue_msg 4
Received continue_msg 5
您可以查看TBB 论坛网站上的答案,了解使用 mutlifunction_nodes 执行此操作的另一种方法。