oops
This commit is contained in:
parent
feb712cfee
commit
4a988cc171
2 changed files with 70 additions and 0 deletions
25
BoundedBuffer.cpp
Normal file
25
BoundedBuffer.cpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "semaphore.h"
|
||||
#include "BoundedBuffer.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void BoundedBuffer::push(string item){
|
||||
|
||||
empty->P();
|
||||
mutex->P();
|
||||
data.push_back(item);
|
||||
mutex->V();
|
||||
full->V();//increment the number of full slots now.
|
||||
}
|
||||
|
||||
string BoundedBuffer::pop(){
|
||||
string item = data.back();
|
||||
full->P();
|
||||
mutex->P();
|
||||
data.pop_back();
|
||||
mutex->V();
|
||||
empty->V();
|
||||
return item;
|
||||
}
|
||||
|
45
BoundedBuffer.h
Normal file
45
BoundedBuffer.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
|
||||
|
||||
#ifndef _BoundedBuffer_H_
|
||||
#define _BoundedBuffer_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
//#include <semaphore.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//extern unsigned int n, b, w;
|
||||
|
||||
class BoundedBuffer{
|
||||
|
||||
int b_val;
|
||||
|
||||
Semaphore *full = new Semaphore(0); //initialized to 0, Since there are 0 full slots
|
||||
Semaphore *empty;// = new Semaphore(b); //initialized to b, Since all the slots are empty
|
||||
Semaphore *mutex = new Semaphore(1);
|
||||
|
||||
vector<string> data;
|
||||
|
||||
|
||||
public:
|
||||
BoundedBuffer(){empty = new Semaphore(100);}//default b value
|
||||
|
||||
BoundedBuffer(int b){
|
||||
b_val = b;
|
||||
empty = new Semaphore(b_val);
|
||||
}
|
||||
|
||||
int get_val(){return b_val;}
|
||||
|
||||
void set_b(int b){ b_val = b;}
|
||||
|
||||
void push(string item);
|
||||
|
||||
string pop();
|
||||
|
||||
//can't be larger than b strings
|
||||
//b = user input
|
||||
};
|
||||
|
||||
#endif
|
Reference in a new issue