0

在传递一条消息之前,我需要累积 2 条传入消息。continue_node的文档中提到了阈值参数 T,但不清楚它是否总是等于连接的前辈的数量。就我而言,只有一个前任,但我希望 T = 2。这可能吗?

continue_node<continue_msg> count2(g, 1, MessageForwarder());
make_edge(some_message_generator, count2);
T==2; // hopefully true?

MessageForwarder 是一个简单的消息转发器主体(除非有预定义的?),

class MessageForwarder {
    continue_msg operator()(continue_msg /*m*/) {
        return continue_msg();
    }
};

我很高兴听到任何关于这个或可能是另一种制作简单“计数节点”的方法的建议。

4

1 回答 1

1

正如您所推测的那样, 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 执行此操作的另一种方法。

于 2013-07-23T16:07:30.227 回答