-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_threads.cpp
73 lines (58 loc) · 1.45 KB
/
test_threads.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// CPP program to demonstrate multithreading
// using three different callables.
#include <iostream>
#include <thread>
using namespace std;
// A dummy function
void foo(int Z)
{
for (int i = 0; i < Z; i++) {
//cout << "Thread using function"
// " pointer as callable\n";
}
}
// A callable object
class thread_obj {
public:
void operator()(int x)
{
for (int i = 0; i < x; i++);
// cout << "Thread using function"
// " object as callable\n";
}
};
int main()
{
//cout << "Threads 1 and 2 and 3 "
// "operating independently" << endl;
// This thread is launched by using
// function pointer as callable
thread th1(foo, 3);
// This thread is launched by using
// function object as callable
thread th2(thread_obj(), 3);
// Define a Lambda Expression
auto f = [](int x) {
for (int i = 0; i < x; i++);
//cout << "Thread using lambda"
//" expression as callable\n";
};
// This thread is launched by using
// lamda expression as callable
thread th3(f, 3);
// Wait for the threads to finish
// Wait for thread t1 to finish
th1.join();
// Wait for thread t2 to finish
th2.join();
// Wait for thread t3 to finish
th3.join();
/*int n;
cin >> n;
for (int i = 0; i < n; i++) {
int c, w, k;
cin >> c >> w >> k;
cout << (c * k <= w ? "yes" : "no") << endl;
}*/
return 0;
}