2015-11-06 11:01:19 -06:00
|
|
|
|
2015-11-06 12:48:11 -06:00
|
|
|
|
|
|
|
#ifndef _BoundedBuffer_H_
|
|
|
|
#define _BoundedBuffer_H_
|
|
|
|
|
2015-11-06 11:56:25 -06:00
|
|
|
#include <vector>
|
2015-11-06 12:15:31 -06:00
|
|
|
#include <string>
|
2015-11-06 12:48:11 -06:00
|
|
|
//#include <semaphore.h>
|
2015-11-06 11:56:25 -06:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2015-11-09 16:44:58 -06:00
|
|
|
//extern unsigned int n, b, w;
|
2015-11-06 11:01:19 -06:00
|
|
|
|
2015-11-06 11:30:46 -06:00
|
|
|
class BoundedBuffer{
|
2015-11-09 16:44:58 -06:00
|
|
|
|
2015-11-06 11:56:25 -06:00
|
|
|
Semaphore *full = new Semaphore(0); //initialized to 0, Since there are 0 full slots
|
2015-11-09 16:44:58 -06:00
|
|
|
Semaphore *empty;// = new Semaphore(b); //initialized to b, Since all the slots are empty
|
2015-11-06 11:56:25 -06:00
|
|
|
Semaphore *mutex = new Semaphore(1);
|
|
|
|
|
2015-11-06 11:30:46 -06:00
|
|
|
vector<string> data;
|
|
|
|
|
2015-11-09 16:44:58 -06:00
|
|
|
public:
|
2015-11-11 04:56:41 -06:00
|
|
|
BoundedBuffer(){empty = new Semaphore(200);}//default b value
|
2015-11-09 16:44:58 -06:00
|
|
|
|
|
|
|
BoundedBuffer(int b){
|
2015-11-11 04:56:41 -06:00
|
|
|
empty = new Semaphore(b);
|
2015-11-13 10:06:46 -06:00
|
|
|
}
|
2015-11-13 10:02:10 -06:00
|
|
|
void set_empty(int e){
|
|
|
|
delete empty;
|
|
|
|
empty = new Semaphore(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() {
|
|
|
|
return (data.size() == 0);
|
2015-11-13 10:06:46 -06:00
|
|
|
}
|
2015-11-06 11:30:46 -06:00
|
|
|
|
|
|
|
void push(string item);
|
|
|
|
|
2015-11-09 16:44:58 -06:00
|
|
|
string pop();
|
2015-11-06 11:30:46 -06:00
|
|
|
|
|
|
|
//can't be larger than b strings
|
|
|
|
//b = user input
|
2015-11-06 11:56:25 -06:00
|
|
|
};
|
2015-11-06 11:01:19 -06:00
|
|
|
|
2015-11-13 10:02:10 -06:00
|
|
|
#endif
|