-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold.cpp
More file actions
48 lines (36 loc) · 987 Bytes
/
Copy pathold.cpp
File metadata and controls
48 lines (36 loc) · 987 Bytes
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
#include<thread>
#include<chrono>
#include<iostream>
#include <semaphore.h>
using namespace std;
sem_t notEmpty, notFull;
int buffer = 0;
void Producer(){
while(1){
sem_post(¬Full);
std::this_thread::sleep_for (std::chrono::milliseconds(200));
buffer = 10;
cout << "\nProd -> Current Item Produced : " << buffer << endl;
sem_post(¬Empty);
std::this_thread::sleep_for (std::chrono::milliseconds(500));
}
}
void Consumer(){
while(1){
sem_post(¬Empty);
std::this_thread::sleep_for (std::chrono::milliseconds(1200));
cout << "\nCons -> Current Item Consumed : " << buffer << endl;
buffer = 0;
sem_post(¬Full);
std::this_thread::sleep_for (std::chrono::milliseconds(500));
}
}
int main() {
sem_init(¬Empty, 0, 0);
sem_init(¬Full, 0, 5);
thread producer_thread(Producer);
thread consumer_thread(Consumer);
producer_thread.join();
consumer_thread.join();
return 0;
}