A template class that is a graph_node, receiver<Input> and sender<Output>. An overwrite_node is a buffer of a single item that can be over-written.
template < typename T > class overwrite_node;
#include "tbb/flow_graph.h"
This type of node buffers a single item of type T. The value is initially invalid. A try_put will set the value of the internal buffer, and broadcast the new value to all successors. Gets from the node are non-destructive. If the internal value is valid, a try_get will return true and copy the buffer value to the output. If the internal value is invalid, try_get will return false.
Rejection of messages by successors is handled using the protocol described in the Message Passing Protocol.
T must be copy-constructible and assignable.
The example demonstrates overwrite_node as a single-value storage that might be updated. Data can be accessed with direct try_get() call.
#include "tbb/flow_graph.h" #include <chrono> #include <thread> int main() { const int data_limit = 20; int count = 0; tbb::flow::graph g; tbb::flow::function_node< int, int > data_set_preparation(g, tbb::flow::unlimited, []( int data ) { printf("Prepare large data set and keep it inside node storage\n"); return data; }); tbb::flow::overwrite_node< int > overwrite_storage(g); tbb::flow::source_node<int> data_generator(g, [&]( int& v ) -> bool { if ( count < data_limit ) { ++count; v = count; return true; } else { return false; } }, false); tbb::flow::function_node< int > process(g, tbb::flow::unlimited, [&]( const int& data) { int data_from_storage = 0; overwrite_storage.try_get(data_from_storage); printf("Data from a storage: %d\n", data_from_storage); printf("Data to process: %d\n", data); }); tbb::flow::make_edge(data_set_preparation, overwrite_storage); tbb::flow::make_edge(data_generator, process); data_set_preparation.try_put(1); data_generator.activate(); g.wait_for_all(); return 0; }
overwrite_node supports reserving join_node as its successor. See example in the example section of write_once_node.
namespace tbb { namespace flow { template< typename T > class overwrite_node : public graph_node, public receiver<T>, public sender<T> { public: explicit overwrite_node( graph &g ); overwrite_node( const overwrite_node &src ); ~overwrite_node(); // receiver<T> typedef T input_type; typedef sender<input_type> predecessor_type; bool try_put( const input_type &v ); bool register_predecessor( predecessor_type &p ); bool remove_predecessor( predecessor_type &p ); // sender<T> typedef T output_type; typedef receiver<output_type> successor_type; bool register_successor( successor_type &r ); bool remove_successor( successor_type &r ); bool try_get( output_type &v ); bool try_reserve( output_type &v ); bool try_release( ); bool try_consume( ); bool is_valid( ); void clear( ); }; } }
Member | Description |
---|---|
explicit overwrite_node( graph &g ) |
Constructs an object of type overwrite_node with an invalid internal buffer item. |
overwrite_node( const overwrite_node &src ) |
Constructs an object of type overwrite_node that belongs to the graph g with an invalid internal buffer item. The buffered value and list of successors is NOT copied from src. |
~overwrite_node( ) |
Destroys the overwrite_node. |
bool try_put( const input_type &v ) |
Stores v in the internal single item buffer and calls try_put(v) on all successors. Returns: true |
bool register_predecessor( predecessor_type &p ) |
Never rejects puts and therefore does not need to maintain a list of predecessors. Returns: false |
bool remove_predecessor( predecessor_type &p ) |
Never rejects puts and therefore does not need to maintain a list of predecessors. Returns: false |
bool register_successor( successor_type &r ) |
Adds r to the set of successors. If a valid item v is held in the buffer, a task is spawned to call r.try_put(v). Returns: true |
bool remove_successor( successor_type &r ) |
Removes r from the set of successors. Returns: true |
bool try_get( 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_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( ) |
Returns: true |
bool try_consume( ) |
Returns: true |
bool is_valid( ) |
Returns: true if the buffer holds a valid value, otherwise returns false. |
void clear( ) |
Invalidates the value held in the buffer. |