Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

CriticalSectionBlock Class Reference

#include <CriticalSectionBlock.H>


Public Member Functions

 CriticalSectionBlock (critsec_t *critsec)
bool checkonce ()
 ~CriticalSectionBlock ()

Private Attributes

critsec_t * _critsec
char _checkonceHackCounter


Detailed Description

A CriticalSectionBlock locks a critical section for the lifetime of the CriticalSectionBlock.

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;
     }
     // ...
 }
 --------------------------------------------------------------------- 
 

Author:
Henner Zeller <H.Zeller@acm.org>

Definition at line 70 of file CriticalSectionBlock.H.


Constructor & Destructor Documentation

CriticalSectionBlock::CriticalSectionBlock critsec_t *  critsec  )  [inline]
 

Constructor locks the critical section.

Definition at line 83 of file CriticalSectionBlock.H.

CriticalSectionBlock::~CriticalSectionBlock  )  [inline]
 

destructor automatically unlocks the critical section.

Definition at line 98 of file CriticalSectionBlock.H.


Member Function Documentation

bool CriticalSectionBlock::checkonce  )  [inline]
 

don't use. Used internally for the synchronized() { } macro.

Definition at line 93 of file CriticalSectionBlock.H.


Field Documentation

char CriticalSectionBlock::_checkonceHackCounter [private]
 

little hack, to make it possible to implement the synchronized() macro with the for(;;) loop below.

Definition at line 77 of file CriticalSectionBlock.H.

critsec_t* CriticalSectionBlock::_critsec [private]
 

Definition at line 72 of file CriticalSectionBlock.H.


The documentation for this class was generated from the following file:
Generated on Fri Feb 25 08:02:48 2005 for brickos by  doxygen 1.3.9.1