This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce313-mp4pinie64backup/BoundedBuffer.cpp

24 lines
448 B
C++
Raw Normal View History

2015-11-06 11:30:46 -06:00
2015-11-06 12:48:11 -06:00
#include "semaphore.h"
2015-11-06 12:15:31 -06:00
#include "BoundedBuffer.h"
#include <string>
#include <vector>
2015-11-06 11:30:46 -06:00
void BoundedBuffer::push(string item){
2015-11-06 12:48:11 -06:00
empty->P();
2015-11-10 15:47:13 -06:00
mutex->P();
2015-11-06 11:30:46 -06:00
data.push_back(item);
2015-11-10 15:47:13 -06:00
mutex->V();
2015-11-06 12:48:11 -06:00
full->V();//increment the number of full slots now.
2015-11-06 11:30:46 -06:00
}
2015-11-13 10:06:46 -06:00
string BoundedBuffer::pop(){
2015-11-13 10:02:10 -06:00
full->P();
mutex->P();
string item = data.back();
data.pop_back();
mutex->V();
empty->V();
2015-11-13 10:06:46 -06:00
return item;
2015-11-06 11:30:46 -06:00
}