-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_pointer.cpp
More file actions
126 lines (84 loc) · 1.89 KB
/
Copy pathfunction_pointer.cpp
File metadata and controls
126 lines (84 loc) · 1.89 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Topic: Types of Thread Creation in C++11.
// there are 5 different type we can create threads in C++11 using callable objects
// NOTE:
//If we create multiple threads at the same tume it doens't guarantee whihc one will start first
#include<iostream>
#include<thread>
#include<chrono>
#include<algorithm>
using namespace std;
using namespace std :: chrono;
//1. Function pointer
// this is the very basic form of thred creation
void fun(int x){
while(x --> 0){
cout<<x<<endl;
}
}
int main(){
std:: thread t1(fun,11);
t1.join();
return 0;
}
//2.Lambda Function
//Example:
// int main(){
// //other method
// // auto fun =[](int x){
// // while(x -->0){
// // cout<<x<<endl;
// // }
// // };
// // std::thread t(fun, 11);
// //we can directly inject lambda at thread creation time.
// std::thread t([](int x){
// while(x -->0){
// cout<<x<<endl;
// }
// }, 11);
// t.join();
// return 0;
// }
// 3. Functor (Function Object)
// class Base{
// public:
// void operator () (int x){
// while(x -->0){
// cout<<x<<endl;
// }
// }
// };
// int main(){
// std::thread t((Base ()),11); // Functor calling
// t.join();
// return 0;
// }
// 4. Non-static member function
// class Base{
// public:
// void run(int x){
// while(x-- >0){
// cout<<x<<endl;
// }
// }
// };
// int main(){
// Base b;
// std :: thread t (&Base :: run,&b,11); // in orde to call non-static member we need object hence, b is used
// t.join();
// return 0;
// }
// 5. Static member fuction
// class Base{
// public:
// static void run(int x){
// while(x-- >0){
// cout<<x<<endl;
// }
// }
// };
// int main(){
// std::thread t(&Base:: run, 11);
// t.join();
// return 0;
// }