-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy paththread_ex2.cpp
49 lines (42 loc) · 962 Bytes
/
thread_ex2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <thread>
using namespace std;
class A {
public:
A(int ID, int Iters)
: id(ID), iterations(Iters)
{}
// overloaded operator ()
void operator() () {
for(auto i=0; i<iterations; i++)
cout << "Counter " << id << " has value: " << i << endl;
cout << endl;
}
// Just a function to call from a thread
void echo(int times) {
for(auto i=0; i<times; i++)
cout << "echoing ... " << i+1 << " ...\n";
}
private:
int id;
int iterations;
};
int main()
{
// Uniform initialization syntax
thread th1{ A{1, 5} };
// Standard syntax
A a(2, 3);
thread th2(a);
// Temporary
thread th3( A(3, 4) );
// Just create a thread and call the echo() member function of class A
A Obj(1, 5);
thread th4( &A::echo, &Obj, 5);
// Join them
th1.join();
th2.join();
th3.join();
th4.join();
return 0;
}