-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_threadFunction.cpp
More file actions
52 lines (41 loc) · 1.33 KB
/
3_threadFunction.cpp
File metadata and controls
52 lines (41 loc) · 1.33 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
/*
We can create a function and pass it as a parameter to the thread.
*/
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void threadFunction_1(int parameter1, int parameter2, int parameterN)
{
while (1)
{
cout<< "\nthreadFunction_1"<<endl;
cout<< "parameter1: "<< parameter1 <<endl;
cout<< "parameter2: "<< parameter2 <<endl;
cout<< "parameterN: "<< parameterN <<endl;
this_thread::sleep_until(chrono::system_clock::now() + chrono::seconds(1)); // sleep 1 sec.
}
}
void threadFunction_2(int parameter1, int parameter2, int parameterN)
{
while (1)
{
cout<< "\nthreadFunction_2"<< endl;
cout<< "parameter1: "<< parameter1 <<endl;
cout<< "parameter2: "<< parameter2 <<endl;
cout<< "parameterN: "<< parameterN <<endl;
this_thread::sleep_until(chrono::system_clock::now() + chrono::seconds(1)); // sleep 1 sec.
}
}
int main()
{
int parameter1 = 10;
int parameter2 = 20;
int parameterN = 30;
cout<< "We can pass more than 3 parameteer to thread"<<endl;
thread t1(&threadFunction_1, parameter1, parameter2, parameterN);
thread t2(&threadFunction_2, parameter1*2, parameter2*2, parameterN*2);
t1.join();
t2.join();
return 0;
}