All exercises
- You can download an archive of the solution [ here ]
Remarks :
- The main problem in that exercise is to determine what must be protected wihtin critical sections, so that threads are not in conflict when they read/write data from resources.
- In the present case, there is a single object that is used by all the threads : the map containing the time list of all clients.
- The solution consists in :
- creating a new class that contains this map (e.g. TimePool)
- to define methods in that class, which manipulate the map accordingly to the processes that must be done when receiving a request.
- all these methods are synchronized (it may cause problem of performance but it is a reliable solution).
- It implies that this class contains methods to :
- register a new client : create a new entry in the map for the new client, thus a new time list,
- store a time : get the time list from a client id and add a new time to this list,
- get the average : get the time list from a client id and compute the average of the times in the list.
- get the miniumum/maximum time : get the time list from a client id and compute the minimum/maximum out of the times in the list.
- get the last time stored : for a gven list of client ids, get their last time stored.
- After defining that class, the threads become "dumb": they DO NOT manipulate the map directly, they ask to the TimePool for.
- More generally, using several threads always imply to make them dumb:
- They never do something by themsleves.
- They always ask for services to shared objects, by calling their methods, and the code of these methods is totally or partially protected by a mutex.