-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMulti_Threads_2.cpp
44 lines (37 loc) · 1.25 KB
/
Multi_Threads_2.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
// 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-1 : 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-2 : function object as callable\n";
}
};
int main()
{
cout << "Threads 1 and 2 and 3 operating independently" << endl;
thread th1(foo, 3); // This thread is launched by using function pointer as callable
thread th2(thread_obj(), 3); // This thread is launched by using function object as callable
// Define a Lambda Expression
auto f = [](int x)
{
for (int i = 0; i < x; i++)
cout << "Thread-3 : lambda expression as callable\n";
};
thread th3(f, 3); // This thread is launched by using lamda expression as callable
th1.join(); // Wait for the threads to finish Wait for thread t1 to finish
th2.join(); // Wait for thread t2 to finish
th3.join(); // Wait for thread t3 to finish
return 0;
}