test_tbb.cxx
Go to the documentation of this file.
1 #include <tbb/flow_graph.h>
2 #include <iostream>
3 #include <tuple>
4 
5 using namespace std;
6 namespace dfp = tbb::flow;
7 
8 
9 template<typename DataType>
10 struct number_source {
12  typedef std::vector<data_type> vector_type;
13  vector_type dat;
14  number_source(const vector_type& d) : dat(d) {}
15  bool operator()(data_type& out) {
16  cerr << "number_source with " << dat.size() << " element " << endl;
17  if (dat.empty()) {
18  cerr << "\tempty" << endl;
19  return false;
20  }
21  out = dat.front();
22  dat.erase(dat.begin());
23  cerr << "\treturning value: " << out << endl;
24  return true;
25  }
26 };
27 
28 typedef dfp::multifunction_node<int, tbb::flow::tuple<float> > int2float_node;
29 
30 struct I2Fcaster {
31  void operator()(const int& in, int2float_node::output_ports_type& op) {
32  float f(in);
33  bool ok = std::get<0>(op).try_put(f);
34  if (ok) {
35  cerr << "cast " << in << " to " << f << endl;
36  }
37  else {
38  cerr << "FAILED " << in << " to " << f << endl;
39  }
40  }
41 };
42 
43 #include <chrono>
44 #include <thread>
45 void msleep(int msec)
46 {
47  std::this_thread::sleep_for(std::chrono::milliseconds(msec));
48 }
49 
50 int main() {
51  dfp::graph g;
52  std::vector<int> numbers{5,4,3,2,1,0};
53  dfp::source_node<int> number_source_node(g, number_source<int>(numbers), false);
54  dfp::function_node<int, int> int_chirp_node(g, dfp::unlimited, [](const int &v) {
55  cerr << "i value: " << v << endl;
56  msleep(v*100);
57  cerr << "...woke, returning i=" << v << endl;
58  return v;
59  });
60  dfp::function_node<float,float> float_chirp_node(g, dfp::unlimited, [](const float &v) {
61  cerr << "f value: " << v << endl;
62  msleep(v*100);
63  cerr << "...woke, returning f=" << v << endl;
64  return v;
65  });
66  int2float_node i2fcaster_node(g, dfp::unlimited, I2Fcaster());
67 
68  cerr << "make edges" << endl;
69  make_edge(number_source_node, int_chirp_node);
70  make_edge(int_chirp_node, i2fcaster_node);
71  make_edge(dfp::output_port<0>(i2fcaster_node), float_chirp_node);
72 
73  cerr << "Activate source" << endl;
74  number_source_node.activate();
75  cerr << "Waiting for graph" << endl;
76  g.wait_for_all();
77  return 0;
78 }
void operator()(const int &in, int2float_node::output_ports_type &op)
Definition: test_tbb.cxx:31
def graph(desc, maker=maker)
Definition: apa.py:294
number_source(const vector_type &d)
Definition: test_tbb.cxx:14
STL namespace.
void msleep(int msec)
Definition: test_tbb.cxx:45
static const double g
Definition: Units.h:145
dfp::multifunction_node< int, tbb::flow::tuple< float > > int2float_node
Definition: test_tbb.cxx:28
vector_type dat
Definition: test_tbb.cxx:13
int main()
Definition: test_tbb.cxx:50
DataType data_type
Definition: test_tbb.cxx:11
bool operator()(data_type &out)
Definition: test_tbb.cxx:15
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:100
std::vector< data_type > vector_type
Definition: test_tbb.cxx:12
QTextStream & endl(QTextStream &s)