-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
152 lines (127 loc) · 3.94 KB
/
main.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Created by hato0 on 23.05.2022.
//
#include <pthread.h>
#include <cstdio> //for C use stdio.h
/* C */
/* #define nullptr NULL */
/* Programm that shows the consumer producer problem with
* 2 Consumers
* 3 Producers
* and with notify all
* (in C++)
* --------------------
* To only use notify change 'pthread_cond_broadcast(&dieCondition);' to
* pthread_cond_signal(&dieCondition);
* --------------------
* !!!Main program runs indefinitely!!!
* !!!Stop it manually!!!
* --------------------
* Compiler Flag pthread is needed:
* add
* SET(CMAKE_CXX_FLAGS -pthread)
* to your CMakeLists.txt
* --------------------
* C++ Standard 14
* to run in C */
/* The Output is a bit messy with 2 Mutex / one for each Function */
/* A mutex protecting put. */
//pthread_mutex_t putMutex;
/* A mutex protecting get. */
//pthread_mutex_t getMutex;
/* Mutex for put and get Function */
pthread_mutex_t getputMutex;
/* Main condition */
pthread_cond_t dieCondition;
/* Consumer Threads */
pthread_t consumer1;
pthread_t consumer2;
/* Producer Threads */
pthread_t producer1;
pthread_t producer2;
pthread_t producer3;
/* Variable if data can be consumed */
bool available = false;
/* C */
/* int available = 0; */
/* Data and Zahl || Zahl put in Data */
int data;
void *produce(void *);
void *consume(void *);
/* Put. Puts Zahl into data */
void* put(int zahl) {
int id = pthread_self();
while(available) {
printf("[put]:: Thread %d geraet in WAITING \n",id);
pthread_cond_wait(&dieCondition,&getputMutex);
printf("[put]:: Thread %d macht weiter \n", id);
}
data = zahl;
printf("[put]:: Thread %d hat %d in Data eingetragen \n", id, zahl);
/* C */
/* available = 1 */
available = true;
printf("[put]:: Thread %d available = true gesetzt (Data ist voll/es kann nix neues eingetragen werden) \n", id);
pthread_cond_broadcast(&dieCondition);
//pthread_cond_signal(&dieCondition);
return nullptr;
}
/* Get. Returns data */
int get() {
int id = pthread_self();
while(!available) {
printf("[get]:: Thread %d geraet in WAITING \n",id);
pthread_cond_wait(&dieCondition,&getputMutex);
printf("[get]:: Thread %d macht weiter \n", id);
}
/* C */
/* available = 0 */
available = false;
printf("[get]:: Thread %d hat available = false gesetzt (Data konnte ausgelesen werden) \n", id);
pthread_cond_broadcast(&dieCondition);
//pthread_cond_signal(&dieCondition);
return data;
}
/* The main program. */
int main() {
pthread_mutex_init(&getputMutex, nullptr);
//pthread_mutex_init(&getMutex, nullptr);
//pthread_mutex_init(&putMutex, nullptr);
pthread_cond_init(&dieCondition, nullptr);
/* Creates new threads. The new threads will run produce function. */
pthread_create(&producer1,nullptr,&produce,nullptr);
pthread_create(&producer2,nullptr,&produce,nullptr);
pthread_create(&producer3,nullptr,&produce,nullptr);
/* Creates new threads. The new threads will run consume function. */
pthread_create(&consumer1,nullptr,&consume,nullptr);
pthread_create(&consumer2,nullptr,&consume,nullptr);
/* main thread is blocked from finishing */
while(true) {
/* Endless Loop */
}
}
/* Consumes data continuously */
void *consume(void * unused) {
int returnedZahl;
int id = pthread_self();
/* Endless Loop */
while(true) {
pthread_mutex_lock(&getputMutex);
returnedZahl = get();
printf("[consume]:: Thread %d hat Zahl %d konsumiert\n", id, returnedZahl);
pthread_mutex_unlock(&getputMutex);
}
}
/* Produces new zahl continuously and puts it into data */
void *produce(void * unused) {
int zahl;
int id = pthread_self();
/* Endless Loop */
while(true) {
zahl++;
pthread_mutex_lock(&getputMutex);
printf("[produce]:: Thread %d hat Zahl %d produziert\n", id, zahl);
put(zahl);
pthread_mutex_unlock(&getputMutex);
}
}