#include <CriticalSectionBlock.H>
Public Member Functions | |
CriticalSectionBlock (critsec_t *critsec) | |
bool | checkonce () |
~CriticalSectionBlock () | |
Private Attributes | |
critsec_t * | _critsec |
char | _checkonceHackCounter |
The CriticalSectionBlock gets a critical section and initially enters it, i.e. the constructor blocks until the section is free. The critical section is unlocked in the destructor.
The actual trick is, that if the CriticalSectionBlock is created as automatic variable on the stack (complicated expression for local variable), C++ implicitly calls the destructor, whenever we leave the block it is created in.
Leaving the block may be due to a regular return, or an Exception or just falling off the edge, thus this automatically takes care of cleaning up the section which is especially handy if we expect Exceptions. Never forgotten-to-unlock critical sections again .. (isn't this cool ?)
Usage like:
-------------------------------------------------------------------- critsec_t someCriticalSection; void foo () { CriticalSectionBlock criticalSection(&someCriticalSection); //.. critical section protected ... myCriticalDataStructure = 42; // criticalSection is automatically released } ---------------------------------------------------------------------
There is as well a macro available which utilizes this class to make it really easy to use and to read. People who have programmed Java reckognize this; use this in favour of the plain CriticalSectionBlock because it improves readability a lot:
--------------------------------------------------------------------- critsec_t someCriticalSection; void foo () { // ... synchronized (&someCriticalSection) { //.. critical section protected... myCriticalDataStructure = 42; } // ... } ---------------------------------------------------------------------
Definition at line 70 of file CriticalSectionBlock.H.
|
Constructor locks the critical section. Definition at line 83 of file CriticalSectionBlock.H. |
|
destructor automatically unlocks the critical section. Definition at line 98 of file CriticalSectionBlock.H. |
|
don't use. Used internally for the synchronized() { } macro. Definition at line 93 of file CriticalSectionBlock.H. |
|
little hack, to make it possible to implement the synchronized() macro with the for(;;) loop below. Definition at line 77 of file CriticalSectionBlock.H. |
|
Definition at line 72 of file CriticalSectionBlock.H. |