test_thread.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <chrono>
4 #include <thread>
5 void msleep(int msec)
6 {
7  std::this_thread::sleep_for(std::chrono::milliseconds(msec));
8 }
9 
10 using namespace std;
11 
12 struct Fun {
13  int n;
14  Fun(int n):n(n) {}
15  int operator()(int x, bool dec) {
16  while (x>0) {
17  int old_n = n;
18  if (dec) {
19  n = x;
20  }
21  stringstream msg;
22  msg << this << " n=" << n << " (was:" << old_n << ") x=" << x << "\n";
23  cerr << msg.str();
24  --x;
25  msleep(x);
26  }
27  return 0;
28  }
29 };
30 
31 int main() {
32 
33  Fun f1(1);
34 
35  std::thread t1(f1,42,true);
36  std::thread t2(f1,69,false);
37  t1.join();
38  t2.join();
39 
40  return 0;
41 }
int main()
Definition: test_thread.cxx:31
void msg(const char *fmt,...)
Definition: message.cpp:107
int operator()(int x, bool dec)
Definition: test_thread.cxx:15
STL namespace.
EmPhysicsFactory f1
QTextStream & dec(QTextStream &s)
void msleep(int msec)
Definition: test_thread.cxx:5
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:100
int n
Definition: test_thread.cxx:13
list x
Definition: train.py:276
std::size_t n
Definition: format.h:3399
Fun(int n)
Definition: test_thread.cxx:14