CSE 305 - Tutorial 6
In this tutorial, you will be asked to implement classes/functions described below. The exact definitions and automated tests are given to you in the following archive.
Your task is to fill the implementations of the functions in td6.cpp and submit it on this page by 23:59 May 21. The solution will be graded first by autograder (40% of the grade) and then manually (remaining 60% of the grade). You are allowed (and encouraged) to revise your code based on the output of autograder and resubmit again before the deadline. The demo code is available here.
Implement a safe unbounded queue
SafeUnboundedQueue
(methodspush
andpop
) similar to the previous TD but with blockingpop
and without busy waiting. Use condition variables as explained in the lecture. Detailed specification is given in td6.cpp.Implement a class
Account
(full specification in td6.cpp) which can contain an amount of money only between0
andmax_amount
. The class should have methodsadd
andwithdraw
to add and withdraw money, respectively. In the case operation is not possible (the amount will get below zero or beyond the bound), the methods should wait.For the
Account
class from the previous exercise, implement the methodbool change_max(new_max)
which changes the maximal allowed amount. If the current amount is greater thannew_max
, it should do nothing and return false. Otherwise, should returntrue
. If some of the previously impossible transfers become possible, they should stop waiting and proceed.
Upload your file td6.cpp
:
The following exercise is optional.
- Starting new threads is not cheap, so if our big task is subdivided
into a big number of subtasks, it would be natural to reuse some of the
threads. This can be done by creating a thread pool. In this question,
you will be asked to implement a class
SimplePool
(details in pool.cpp) based in the thread-safe unbounded queue (see Q1) and has an attributenum_threads
. Each new task in enqueued into the queue. The pool should managenum_threads
threads, each of which is taking new tasks from the queue and completes them in a while-loop. Methods should includepush
- pushes a new task;stop
- finishes current computation and joins all working threads.
num_workers
of such tasks into the task queue.
Upload your file pool.cpp
: