-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrera.cpp
More file actions
37 lines (31 loc) · 725 Bytes
/
barrera.cpp
File metadata and controls
37 lines (31 loc) · 725 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
#include "barrera.h"
Barrera::Barrera(int x) : N(x) {
sem_init(&this->b1,0,0);
sem_init(&this->b2,0,0);
//this->N = x;
}
void Barrera::wait() {
// Primera fase
this->mtx.lock();
this->n++;
if (this->n == this->N) {
// Hago n signals a b1
for(int i = 0; i < N; i++) {
sem_post(&this->b1);
}
}
this->mtx.unlock();
sem_wait(&this->b1);
// Segunda fase
this->mtx.lock();
n--;
cout << "Faltan " << n << " para liberar la barrera" << endl;
if (n == 0) {
// Hago n signals a b2
for(int i = 0; i < N; i++) {
sem_post(&this->b2);
}
}
this->mtx.unlock();
sem_wait(&this->b2);
}