Files
test/cpp/tbb/message_flow_graph_example.cpp
2021-03-25 08:17:48 -04:00

74 lines
1.5 KiB
C++

/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:cppflags=g:Iboost.g:Itbb
VIM: let b:ldflags=g:Lboost.g:Ltbb
VIM: let b:ldlibpath=g:Bboost.g:Btbb
VIM: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <cstdio>
#include <tbb/flow_graph.h>
using namespace tbb::flow;
struct square {
int operator()(int v) { return v*v; }
};
struct cube {
int operator()(int v) { return v*v*v; }
};
class sum {
int &my_sum;
public:
sum( int &s ) : my_sum(s) {}
int operator()( tuple< int, int > v ) {
my_sum += get<0>(v) + get<1>(v);
return my_sum;
}
};
int main()
{try{
int result = 0;
graph g;
broadcast_node<int> input(g);
function_node<int,int> squarer( g, unlimited, square() );
function_node<int,int> cuber( g, unlimited, cube() );
join_node< tuple<int,int>, queueing > join( g );
function_node<tuple<int,int>,int>
summer( g, serial, sum(result) );
make_edge( input, squarer );
make_edge( input, cuber );
make_edge( squarer, get<0>( join.input_ports() ) );
make_edge( cuber, get<1>( join.input_ports() ) );
make_edge( join, summer );
for (int i = 1; i <= 10; ++i)
input.try_put(i);
g.wait_for_all();
printf("Final result is %d\n", result);
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}