-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.cpp
63 lines (52 loc) · 1.12 KB
/
queue.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
#include "queue.h"
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;
Queue::Queue(int size): maxSize(size)
{
queueArray = new int[size];
start = 0;
end = -1;
count = 0;
pthread_mutex_init (&mtx, NULL);
}
Queue::~Queue()
{
delete queueArray;
pthread_mutex_destroy(&mtx);
}
int Queue::getMaxSize() //epistrefei to megisto megethos ths ouras
{
return maxSize;
}
int Queue::getCount() //epistrefei to megethos ths wras auth th stigmi
{
return count;
}
void Queue::push(int item) //prosthetei ena int sto telos ths ouras
{
pthread_mutex_lock(&mtx);
while (count >= maxSize) //ama einai gemati perimenw signal
{
pthread_cond_wait(&cond_nonfull, &mtx);
}
end = (end + 1) % maxSize; //epomeno stoixeio ouras
queueArray[end] = item;
count++;
pthread_mutex_unlock(&mtx);
}
int Queue::pop()
{
int data = 0;
pthread_mutex_lock(&mtx);
while (count <= 0) //ama einai adeia perimenw signal
{
pthread_cond_wait(&cond_nonempty, &mtx);
}
data = queueArray[start];
start = (start + 1) % maxSize;
count--;
pthread_mutex_unlock(&mtx);
return data;
}