C12Adapter Opensource C++ Interface
MCriticalSection Class Reference

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...
 

Detailed Description

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:

MCriticalSection criticalSection;
...
void MyFunction()
{
MCriticalSection::Locker lock(criticalSection);
... // do any protected operation
// critical section will be unlocked here automatically
}

A typical error is to omit variable declaration in the locker like this:

MCriticalSection::Locker(criticalSection); // ERROR! critical section does not extend scope
... // THIS WILL NOT BE PROTECTED BY CRITICAL SECTION!

in which case the locker will not do what it is designed for.

Constructor & Destructor Documentation

MCriticalSection::MCriticalSection ( )

Constructor of the critical section.

Precondition
There should be enough of system resources, otherwise the behavior is undefined.
MCriticalSection::~MCriticalSection ( )

Destructor.

Precondition
No object should wait or lock this particular critical section, otherwise the behavior is undefined.

Member Function Documentation

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.

Precondition
Critical section should be valid, and there should be enough system resources, otherwise the behavior is undefined.
See also
MCriticalSection::Locker - more convenient way of dealing with the critical section
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.

Precondition
Critical section should be valid, and there should be enough system resources, otherwise the behavior is undefined.
See also
MCriticalSection::Lock - wait until the section is acquired
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.

Precondition
Critical section should be previously locked by this object, otherwise the behavior is undefined.
See also
MCriticalSection::Locker - more convenient way of dealing with the critical section