-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_multithread.cpp
More file actions
52 lines (43 loc) · 1.18 KB
/
2_multithread.cpp
File metadata and controls
52 lines (43 loc) · 1.18 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
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
/*
thread1, thread2 and thread3 are running concurrently.
*/
void Thread1()
{
while (1)
{
this_thread::sleep_until(chrono::system_clock::now() + chrono::seconds(1)); //running every 1 second
cout<< "Thread 1 running\n" << endl;
}
}
void Thread2()
{
while (1)
{
this_thread::sleep_until(chrono::system_clock::now() + chrono::seconds(5)); //running every 5 second
cout<< "Thread 2 running\n" << endl;
}
}
void Thread3()
{
while (1)
{
this_thread::sleep_until(chrono::system_clock::now() + chrono::seconds(7)); //running every 7 second
cout<< "Thread 3 running\n" << endl;
}
}
int main()
{
thread t1(Thread1);
thread t2(Thread2);
thread t3(Thread3);
t1.join();
t2.join();
t3.join();
cout<< "Main is running\n" << endl; // this code will not work because join() of threads t1, t2 and t3 is
// defined before this line. this line would work if it was defined below this line.
return 0;
}