Question

I need to accumulate 2 incoming messages before passing on a single message. The documentation of the continue_node mentions the threshold parameter T but is not clear on whether it always equals the number of connected predecessors. In my case there is only one predecessor but I'd like T = 2. Is this possible?

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

Where the MessageForwarder is a simple message forwarder body (unless there is a predefined one?),

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

I'd be glad to hear any advice on this or possibly another way of making a simple "counting node".

Was it helpful?

Solution

As you surmised, the continue_node doesn't fire the body until it receives a number of continue_msgs equivalent to the number of predecessors. If you specify a predecessor count in the constructor, the count is initialized to that number, and adding an edge to the node increments that count.

I am including a small program demonstrating this:

#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();
}

I initialized the continue_node counting_node2 to 1, and added an edge to it. The output should be

Received continue_msg 1
Received continue_msg 2
Received continue_msg 3
Received continue_msg 4
Received continue_msg 5

You can look at an answer on the TBB forum site for another way to do this with mutlifunction_nodes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top