#include <Synchronized.h>
Public Member Functions | |
Synchronized (SEM_ID) | |
Synchronized (ReentrantSemaphore &) | |
virtual | ~Synchronized () |
Provide easy support for critical regions.
A critical region is an area of code that is always executed under mutual exclusion. Only one task can be executing this code at any time. The idea is that code that manipulates data that is shared between two or more tasks has to be prevented from executing at the same time otherwise a race condition is possible when both tasks try to update the data. Typically semaphores are used to ensure only single task access to the data.
Synchronized objects are a simple wrapper around semaphores to help ensure that semaphores are always unlocked (semGive) after locking (semTake).
You allocate a Synchronized as a local variable, not on the heap. That makes it a "stack object" whose destructor runs automatically when it goes out of scope. E.g.
{ Synchronized _sync(aReentrantSemaphore); ... critical region ... }
|
explicit |
Synchronized class deals with critical regions. Declare a Synchronized object at the beginning of a block. That will take the semaphore. When the code exits from the block it will call the destructor which will give the semaphore. This ensures that no matter how the block is exited, the semaphore will always be released. Use the CRITICAL_REGION(SEM_ID) and END_REGION macros to make the code look cleaner (see header file)
semaphore | The semaphore controlling this critical region. |
|
virtual |
This destructor unlocks the semaphore.