Xmipp  v3.23.11-Nereus
example.cpp
Go to the documentation of this file.
1 #include <ctpl.h>
2 #include <iostream>
3 #include <string>
4 
5 
6 
7 void first(int id) {
8  std::cout << "hello from " << id << ", function\n";
9 }
10 
11 void aga(int id, int par) {
12  std::cout << "hello from " << id << ", function with parameter " << par <<'\n';
13 }
14 
15 struct Third {
16  Third(int v) { this->v = v; std::cout << "Third ctor " << this->v << '\n'; }
17  Third(Third && c) { this->v = c.v; std::cout<<"Third move ctor\n"; }
18  Third(const Third & c) { this->v = c.v; std::cout<<"Third copy ctor\n"; }
19  ~Third() { std::cout << "Third dtor\n"; }
20  int v;
21 };
22 
23 void mmm(int id, const std::string & s) {
24  std::cout << "mmm function " << id << ' ' << s << '\n';
25 }
26 
27 void ugu(int id, Third & t) {
28  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
29  std::cout << "hello from " << id << ", function with parameter Third " << t.v <<'\n';
30 }
31 
32 int main(int argc, char **argv) {
33  ctpl::thread_pool p(2 /* two threads in the pool */);
34 
35  std::future<void> qw = p.push(std::ref(first)); // function
36  p.push(first); // function
37  p.push(aga, 7); // function
38 
39  {
40  struct Second {
41  Second(const std::string & s) { std::cout << "Second ctor\n"; this->s = s; }
42  Second(Second && c) { std::cout << "Second move ctor\n"; s = std::move(c.s); }
43  Second(const Second & c) { std::cout << "Second copy ctor\n"; this->s = c.s; };
44  ~Second() { std::cout << "Second dtor\n"; }
45  void operator()(int id) const {
46  std::cout << "hello from " << id << ' ' << this->s << '\n';
47  }
48  private:
49  std::string s;
50  } second(", functor");
51 
52  p.push(std::ref(second)); // functor, reference
53  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
54  p.push(const_cast<const Second &>(second)); // functor, copy ctor
55  p.push(std::move(second)); // functor, move ctor
56  p.push(second); // functor, move ctor
57  p.push(Second(", functor")); // functor, move ctor
58  }
59  {
60  Third t(100);
61 
62  p.push(ugu, std::ref(t)); // function. reference
63  p.push(ugu, t); // function. copy ctor, move ctor
64  p.push(ugu, std::move(t)); // function. move ctor, move ctor
65 
66  }
67  p.push(ugu, Third(200)); // function
68 
69 
70 
71  std::string s = ", lambda";
72  p.push([s](int id){ // lambda
73  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
74  std::cout << "hello from " << id << ' ' << s << '\n';
75  });
76 
77  p.push([s](int id){ // lambda
78  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
79  std::cout << "hello from " << id << ' ' << s << '\n';
80  });
81 
82  p.push(mmm, "worked");
83 
84  auto f = p.pop();
85  if (f) {
86  std::cout << "poped function from the pool ";
87  f(0);
88  }
89  // change the number of treads in the pool
90 
91  p.resize(1);
92 
93  std::string s2 = "result";
94  auto f1 = p.push([s2](int){
95  return s2;
96  });
97  // other code here
98  //...
99  std::cout << "returned " << f1.get() << '\n';
100 
101  auto f2 = p.push([](int){
102  throw std::exception();
103  });
104  // other code here
105  //...
106  try {
107  f2.get();
108  }
109  catch (std::exception & e) {
110  std::cout << "caught exception\n";
111  }
112 
113  // get thread 0
114  auto & th = p.get_thread(0);
115 
116  return 0;
117 }
doublereal * c
auto push(F &&f, Rest &&... rest) -> std::future< decltype(f(0, rest...))>
Definition: ctpl.h:152
int main(int argc, char **argv)
Definition: example.cpp:32
~Third()
Definition: example.cpp:19
void resize(int nThreads)
Definition: ctpl.h:70
Third(Third &&c)
Definition: example.cpp:17
double * f
std::function< void(int)> pop()
Definition: ctpl.h:106
std::thread & get_thread(int i)
Definition: ctpl.h:65
void mmm(int id, const std::string &s)
Definition: example.cpp:23
void first(int id)
Definition: example.cpp:7
void aga(int id, int par)
Definition: example.cpp:11
Third(int v)
Definition: example.cpp:16
Third(const Third &c)
Definition: example.cpp:18
int v
Definition: example.cpp:20
void ugu(int id, Third &t)
Definition: example.cpp:27