#include <system-mutex.h>
When more than one SystemThread needs to access a shared resource, we conrol access by acquiring a SystemMutex. The CriticalSection class uses the C++ scoping rules to automatically perform the required Lock and Unlock operations to implement a Critical Section.
If one wants to treat an entire method call as a critical section, one would do something like,
Class::Method () { CriticalSection cs (mutex); ... }
In this case, the critical section is entered when the CriticalSection object is created, and the critical section is exited when the CriticalSection object goes out of scope at the end of the method.
Finer granularity is achieved by using local scope blocks.
Class::Method () { ... { CriticalSection cs (mutex); } ... }
Here, the critical section is entered partway through the method when the CriticalSection object is created in the local scope block (the braces). The critical section is exited when the CriticalSection object goes out of scope at the end of block.