added more annotations

This commit is contained in:
Aaron Beckmann 2023-12-02 15:29:28 +01:00
parent 04c37c5078
commit 5e7049feb0
4 changed files with 13 additions and 3 deletions

View File

@ -12,6 +12,9 @@ template<typename T> void Consumer<T>::initClass(int numP, Monitor<T> *m, void (
} }
//--------------------------------------- //---------------------------------------
//Semaphore with Number of Resources -> consume N Resources
//get spot of item in queue, take item, activate producers
//process item
template<typename T> void Consumer<T>::run() { template<typename T> void Consumer<T>::run() {
while (numProducts.tryAcquire()) { // While not all numProducts are consumed: while (numProducts.tryAcquire()) { // While not all numProducts are consumed:
T* aux = mon->canGet(); // Get pointer to item in itemQ T* aux = mon->canGet(); // Get pointer to item in itemQ

View File

@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
c[i]->start(); c[i]->start();
} }
//make finished threads wait?
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
p[i]->wait(); p[i]->wait();
for (int i = 0; i < M; i++) for (int i = 0; i < M; i++)

View File

@ -31,8 +31,8 @@ T* Monitor<T>::canPut() {
//consumer tries to take an item: //consumer tries to take an item:
// lock l // lock l
// push produced item to itemQ // wait until items are in itemq
// wake waiting consumer // take item
template<typename T> template<typename T>
T* Monitor<T>::canGet() { T* Monitor<T>::canGet() {
QMutexLocker ml(&l); // Lock entry controlling mutex l QMutexLocker ml(&l); // Lock entry controlling mutex l
@ -56,6 +56,10 @@ void Monitor<T>::donePutting(T *x) {
} }
//----------------------------------------- //-----------------------------------------
//consumer is done with taking an item:
// lock l
// enlarge producer queue by one
// notify a producer of new spot
template<typename T> template<typename T>
void Monitor<T>::doneGetting(T *x) { void Monitor<T>::doneGetting(T *x) {
QMutexLocker ml(&l); // Lock entry controlling mutex l QMutexLocker ml(&l); // Lock entry controlling mutex l

View File

@ -13,6 +13,9 @@ template<typename T> void Producer<T>::initClass(int numP, Monitor<T> *m, T(*pro
} }
//--------------------------------------- //---------------------------------------
//Semaphore with Number of Resources -> create N Resources
//produce item -> get spot in queue -> put item into second queue
//activate consumers
template<typename T> template<typename T>
void Producer<T>::run() { void Producer<T>::run() {
while (numProducts.tryAcquire()) { // While not all numProducts items are produced: while (numProducts.tryAcquire()) { // While not all numProducts items are produced: