Function Nodes Policies#

[flow_graph.function_node_policies]

function_node, multifunction_node, async_node and continue_node can be specified by the Policy parameter, which is represented as a set of tag classes. This parameter affects behavior of node execution.

// Defined in header <oneapi/tbb/flow_graph.h>

namespace oneapi {
namespace tbb {
namespace flow {

    class queueing { /*unspecified*/ };
    class rejecting { /*unspecified*/ };
    class lightweight { /*unspecified*/ };
    class queueing_lightweight { /*unspecified*/ };
    class rejecting_lightweight { /*unspecified*/ };

} // namespace flow
} // namespace tbb
} // namespace oneapi

Each policy class satisfies the CopyConstructible requirements from [copyconstructible] ISO C++ Standard sections.

Queueing#

This policy defines behavior for input messages acceptance. The queueing policy means that input messages that cannot be processed right away are kept to be processed when possible.

Rejecting#

This policy defines behavior for input messages acceptance. The rejecting policy means that input messages that cannot be processed right away are not accepted by the node and it is responsibility of a predecessor to handle this.

Lightweight#

This policy helps to reduce the overhead associated with the execution scheduling of the node.

For functional nodes that have a default value for the Policy template parameter, specifying the lightweight policy results in extending the behavior of the default value of Policy with the behavior defined by the lightweight policy. For example, if the default value of Policy is queueing, specifying lightweight as the Policy value is equivalent to specifying queueing_lightweight.

Example#

The example below shows the application of the lightweight policy to a graph with a pipeline topology. It is reasonable to apply the lightweight policy to the second and third nodes because the bodies of these nodes are small. This allows the second and third nodes to execute without task scheduling overhead. The lightweight policy is not specified for the first node in order to permit concurrent invocations of the graph.

#include "oneapi/tbb/flow_graph.h"

int main() {
    using namespace oneapi::tbb::flow;

    graph g;

    function_node< int, int > add( g, unlimited, [](const int &v) {
        return v+1;
    } );
    function_node< int, int, lightweight > multiply( g, unlimited, [](const int &v) {
        return v*2;
    } );
    function_node< int, int, lightweight > cube( g, unlimited, [](const int &v) {
        return v*v*v;
    } );

    make_edge(add, multiply);
    make_edge(multiply, cube);

    for(int i = 1; i <= 10; ++i)
        add.try_put(i);
    g.wait_for_all();

    return 0;
}