-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsem-print3-threads.cpp
70 lines (60 loc) · 1.09 KB
/
sem-print3-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
//use 3 threads to print numbers serially using semaphore
//
//
#include<iostream>
#include<semaphore.h>
#include<chrono>
#include<thread>
using namespace std;
void* func1(void *);
void* func2(void *);
void* func3(void *);
sem_t sem1,sem2,sem3;
int main()
{
pthread_t t1,t2,t3;
int tid[]={1,2,3};
sem_init(&sem1,0,1);
sem_init(&sem2,0,0);
sem_init(&sem3,0,0);
pthread_create(&t1,NULL,func1,&tid[0]);
pthread_create(&t2,NULL,func2,&tid[1]);
pthread_create(&t3,NULL,func3,&tid[2]);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
return 0;
}
void* func1(void *tid)
{
while(1)
{
sem_wait(&sem1);
cout<<"hello from thread1"<<endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
sem_post(&sem2);
}
return 0;
}
void* func2(void *tid)
{
while(1)
{
sem_wait(&sem2);
cout<<"hello from thread2"<<endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
sem_post(&sem3);
}
return 0;
}
void* func3(void *tid)
{
while(1)
{
sem_wait(&sem3);
cout<<"hello from thread3"<<endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
sem_post(&sem1);
}
return 0;
}