From 4a988cc17197484a727d1eafa31fc5dc8788b952 Mon Sep 17 00:00:00 2001 From: shadow8t4 Date: Tue, 10 Nov 2015 15:56:32 -0600 Subject: [PATCH] oops --- BoundedBuffer.cpp | 25 +++++++++++++++++++++++++ BoundedBuffer.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 BoundedBuffer.cpp create mode 100644 BoundedBuffer.h diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp new file mode 100644 index 0000000..1b191c1 --- /dev/null +++ b/BoundedBuffer.cpp @@ -0,0 +1,25 @@ + +#include "semaphore.h" +#include "BoundedBuffer.h" +#include +#include + +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; +} + \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h new file mode 100644 index 0000000..7c8a06c --- /dev/null +++ b/BoundedBuffer.h @@ -0,0 +1,45 @@ + + +#ifndef _BoundedBuffer_H_ +#define _BoundedBuffer_H_ + +#include +#include +//#include + +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 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