From 5e7049feb095829a3d7fd4fb9524a92e10a8c280 Mon Sep 17 00:00:00 2001 From: Aaron Beckmann Date: Sat, 2 Dec 2023 15:29:28 +0100 Subject: [PATCH] added more annotations --- MonitorExample/consumer.cpp | 3 +++ MonitorExample/main.cpp | 2 +- MonitorExample/monitor.cpp | 8 ++++++-- MonitorExample/producer.cpp | 3 +++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/MonitorExample/consumer.cpp b/MonitorExample/consumer.cpp index 55ca38f..528e052 100644 --- a/MonitorExample/consumer.cpp +++ b/MonitorExample/consumer.cpp @@ -12,6 +12,9 @@ template void Consumer::initClass(int numP, Monitor *m, void ( } //--------------------------------------- +//Semaphore with Number of Resources -> consume N Resources +//get spot of item in queue, take item, activate producers +//process item template void Consumer::run() { while (numProducts.tryAcquire()) { // While not all numProducts are consumed: T* aux = mon->canGet(); // Get pointer to item in itemQ diff --git a/MonitorExample/main.cpp b/MonitorExample/main.cpp index 722589c..e589af5 100644 --- a/MonitorExample/main.cpp +++ b/MonitorExample/main.cpp @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) { c[i]->start(); } - + //make finished threads wait? for (int i = 0; i < N; i++) p[i]->wait(); for (int i = 0; i < M; i++) diff --git a/MonitorExample/monitor.cpp b/MonitorExample/monitor.cpp index 7d1cd74..b1ace5e 100644 --- a/MonitorExample/monitor.cpp +++ b/MonitorExample/monitor.cpp @@ -31,8 +31,8 @@ T* Monitor::canPut() { //consumer tries to take an item: // lock l -// push produced item to itemQ -// wake waiting consumer +// wait until items are in itemq +// take item template T* Monitor::canGet() { QMutexLocker ml(&l); // Lock entry controlling mutex l @@ -56,6 +56,10 @@ void Monitor::donePutting(T *x) { } //----------------------------------------- +//consumer is done with taking an item: +// lock l +// enlarge producer queue by one +// notify a producer of new spot template void Monitor::doneGetting(T *x) { QMutexLocker ml(&l); // Lock entry controlling mutex l diff --git a/MonitorExample/producer.cpp b/MonitorExample/producer.cpp index 427c4eb..4f49812 100644 --- a/MonitorExample/producer.cpp +++ b/MonitorExample/producer.cpp @@ -13,6 +13,9 @@ template void Producer::initClass(int numP, Monitor *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 void Producer::run() { while (numProducts.tryAcquire()) { // While not all numProducts items are produced: