This assignment will be closed on May 21, 2025 (23:59:59).
You must be authenticated to submit your files

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.

  1. Implement a safe unbounded queue SafeUnboundedQueue (methods push and pop) similar to the previous TD but with blocking pop and without busy waiting. Use condition variables as explained in the lecture. Detailed specification is given in td6.cpp.

  2. Implement a class Account (full specification in td6.cpp) which can contain an amount of money only between 0 and max_amount. The class should have methods add and withdraw 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.

  3. For the Account class from the previous exercise, implement the method bool change_max(new_max) which changes the maximal allowed amount. If the current amount is greater than new_max, it should do nothing and return false. Otherwise, should return true. If some of the previously impossible transfers become possible, they should stop waiting and proceed.

Upload your file td6.cpp:

Upload form is only available when connected

The following exercise is optional.

  1. 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 attribute num_threads. Each new task in enqueued into the queue. The pool should manage num_threads threads, each of which is taking new tasks from the queue and completes them in a while-loop. Methods should include
    • push - pushes a new task;
    • stop - finishes current computation and joins all working threads.
    A tricky think is how to stop the threads. You should not use busy waiting (expensive). One option (outlines in pool.cpp) is to create special “stopper task”. If a worker gets such a task, it terminates. Then it will be enough to push num_workers of such tasks into the task queue.

Upload your file pool.cpp:

Upload form is only available when connected