-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircleBuffer.cpp
47 lines (41 loc) · 1.05 KB
/
circleBuffer.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
#include <iostream>
#include <cstdlib>
#include "circleBuffer.h"
CircleBuffer::CircleBuffer(int sz,pthread_cond_t* cne,pthread_cond_t* cnf){
pthread_mutex_init(&mtx, 0);
this->start = 0;
this->end = -1;
this->count = 0;
this->size = sz;
this->data = (int*)malloc(size*sizeof(int));
this->cond_nonempty = cne;
this->cond_nonfull = cnf;
}
void CircleBuffer::place(int dato){
pthread_mutex_lock(&mtx);
while (this->count >= this->size) {
//printf(">> Found Buffer Full \n");
pthread_cond_wait(this->cond_nonfull, &mtx);
}
this->end = (this->end + 1) % this->size;
this->data[this->end] = dato;
this->count++;
pthread_mutex_unlock(&mtx);
}
int CircleBuffer::obtain(){
int data = 0;
pthread_mutex_lock(&mtx);
while (this->count <= 0) {
//printf(">> Found Buffer Empty \n");
pthread_cond_wait(this->cond_nonempty, &mtx);
}
data = this->data[this->start];
this->start = (this->start + 1) % this->size;
this->count--;
pthread_mutex_unlock(&mtx);
return data;
}
CircleBuffer::~CircleBuffer(){
pthread_mutex_destroy(&mtx);
free(this->data);
}