Xmipp  v3.23.11-Nereus
Classes | Functions
example.cpp File Reference
#include <ctpl.h>
#include <iostream>
#include <string>
Include dependency graph for example.cpp:

Go to the source code of this file.

Classes

struct  Third
 

Functions

void first (int id)
 
void aga (int id, int par)
 
void mmm (int id, const std::string &s)
 
void ugu (int id, Third &t)
 
int main (int argc, char **argv)
 

Function Documentation

◆ aga()

void aga ( int  id,
int  par 
)

Definition at line 11 of file example.cpp.

11  {
12  std::cout << "hello from " << id << ", function with parameter " << par <<'\n';
13 }

◆ first()

void first ( int  id)

Definition at line 7 of file example.cpp.

7  {
8  std::cout << "hello from " << id << ", function\n";
9 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 32 of file example.cpp.

32  {
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
double * f
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
void ugu(int id, Third &t)
Definition: example.cpp:27

◆ mmm()

void mmm ( int  id,
const std::string &  s 
)

Definition at line 23 of file example.cpp.

23  {
24  std::cout << "mmm function " << id << ' ' << s << '\n';
25 }

◆ ugu()

void ugu ( int  id,
Third t 
)

Definition at line 27 of file example.cpp.

27  {
28  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
29  std::cout << "hello from " << id << ", function with parameter Third " << t.v <<'\n';
30 }
int v
Definition: example.cpp:20