Reservable single-item nodes

Summary

Reservation support for overwrite_node and write_once_node.

Header

#define TBB_PREVIEW_RESERVABLE_OVERWRITE_NODE 1
#include "tbb/flow_graph.h"

Description

overwrite_node and write_once_node support reservation as a preview feature under TBB_PREVIEW_RESERVABLE_OVERWRITE_NODE macro. Check for TBB_INTERFACE_VERSION>=10001 to detect if the feature is supported.

CAUTION

An application cannot mix reservable and non-reservable single-item nodes; doing so results in undefined behavior, e.g. the code that uses reservable nodes might deadlock. Recompiling all modules with the same value of the TBB_PREVIEW_RESERVABLE_OVERWRITE_NODE macro is strongly recommended.

Input buffering, output buffering, reserving and forwarding policy

Node

Reception Policy

try_get()?

try_reserve()?

Forwarding

overwrite_node

accept

yes

yes

broadcast-push

write_once_node

accept once

yes

yes

broadcast-push

Members

The following table describes the members of overwrite_node and write_once_node classes that change behavior with this preview feature.
Member Description
bool try_reserve( output_type &v )

If the internal buffer is valid, assigns the value to v.

Returns:true if v is assigned to. false if v is not assigned to.

bool try_release( output_type &v )

Returns: true

bool try_consume( output_type &v )

Returns: true

Example

Reservable overwrite_node usage example.

#define TBB_PREVIEW_RESERVABLE_OVERWRITE_NODE 1
#include "tbb/flow_graph.h"

#include <cassert>

typedef int data_type;

int main() {
    static int N = 0;

    tbb::flow::graph g;
    
    tbb::flow::overwrite_node< data_type > overwrite_n(g);
    tbb::flow::buffer_node< data_type > buffer_n(g);

    tbb::flow::join_node< tbb::flow::tuple< data_type, data_type >, tbb::flow::reserving > join_n(g);

    tbb::flow::function_node< tbb::flow::tuple<data_type, data_type> > counter_n(g, tbb::flow::unlimited, [&](const tbb::flow::tuple< data_type, data_type >& arg) { ++N; });

    tbb::flow::make_edge(overwrite_n, tbb::flow::input_port< 0 >(join_n));
    tbb::flow::make_edge(buffer_n, tbb::flow::input_port< 1 >(join_n));
    tbb::flow::make_edge(join_n, counter_n);

    overwrite_n.try_put(1);
    for (int i = 0; i < 100; i++) {
        buffer_n.try_put(1);
    }
   
    g.wait_for_all();

    assert(N == 100);

    return 0;
}

See Also