Monitors Supplement
Notes
- We will discuss two changes to the buffer.cpp code
- Both are using a design pattern or reusable solution to a commonly needed behavior
- A monitor is a solution to mutual exclusion, preventing multiple objects from using a mutex/semaphore/spin-lock/... at the same time.
- A singleton is a solution to the need for one instance of a unique object, accessible everywhere.
- I have implemented the solution to bounded buffer problem with monitors.
- See bufferMon.cpp.
-
class BufferMonitor is a straight forward implementation
- It cleans up the global variables
- All semaphores related to the problem are contained in it (except printing.)
- In addition, the global counts are held in this as well.
- And the Queue
-
GetMessage and PostMessage methods are now part of this
- A global instance of this needs to be declared and care must be taken to assure that only one instance exists.
- But that will be cleaned up soon.
- The
PrintMonitor also collects the code/data associated with the unique print problem
- But this has been implemented as a singleton
- This assures that only one instance of this monitor is available.
- This should have been done with the BufferMonitor as well, but I did not want to get too complex.
- This pattern relies on private constructors and static classes members
- Since the constructor is private, it can only be called from a class member
- Since
Get is static,
- it can be called without an existing member of the class
- But since there is a static instance contained in this member, it will be constructed
- And since this is a member function, the static member can be constructed.