C12Adapter Opensource C++ Interface
|
System independent lightweight synchronization object. More...
Classes | |
class | Locker |
Class that helps dealing with critical sections within a single execution scope. More... | |
Public Member Functions | |
MCriticalSection () | |
Constructor of the critical section. More... | |
~MCriticalSection () | |
Destructor. More... | |
void | Lock () const |
Lock the critical section for exclusive usage of resources. More... | |
bool | TryLock () const |
Attempt to acquire a lock on the critical section for exclusive usage of resources. More... | |
void | Unlock () const |
Unlock the critical section. More... | |
System independent lightweight synchronization object.
Critical section resembles the interface of MSynchronizer object, however it is not derived from it due to an implementation detail. Critical section works on the thread level only, one cannot use critical sections to synchronize processes. Different from synchronizer objects, critical sections are implemented in a way that if multithreading is not defined, they can still be used in the source code, however they will perform no action. Critical section is reentrant on a per-thread basis, therefore it can be entered multiple times from the same thread, in which case it shall be left from this thread the same number of times in order for the critical section to be released.
Critical section is very often used with the Lock helper class as the following:
A typical error is to omit variable declaration in the locker like this:
in which case the locker will not do what it is designed for.
MCriticalSection::MCriticalSection | ( | ) |
Constructor of the critical section.
MCriticalSection::~MCriticalSection | ( | ) |
Destructor.
void MCriticalSection::Lock | ( | ) | const |
Lock the critical section for exclusive usage of resources.
If the critical section is locked by another thread, the call will wait until the resource is freed, and then lock the section.
Critical section can be locked multiple times by the same thread, in which case it shall be unlocked by the same number of times for the critical section to become released.
bool MCriticalSection::TryLock | ( | ) | const |
Attempt to acquire a lock on the critical section for exclusive usage of resources.
This method never waits. If the critical section is locked by another thread, the call will return false immediately, otherwise it will lock the section. If the call returns false, Unlock shall not be called to release the section.
Critical section can be locked multiple times by the same thread, in which case it shall be unlocked by the same number of times for the critical section to become released.
void MCriticalSection::Unlock | ( | ) | const |
Unlock the critical section.
If the critical section is entered multiple times by the same thread, it will not be released until unlocked the same number of times.