Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

CriticalSectionBlock Class Reference

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

#include <CriticalSectionBlock.H>

Collaboration diagram for CriticalSectionBlock:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 CriticalSectionBlock (critsec_t *critsec)
 Constructor locks the critical section.

bool checkonce ()
 don't use.

 ~CriticalSectionBlock ()
 destructor automatically unlocks the critical section.


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.

References critsec_t, and enter_critical_section().

CriticalSectionBlock::~CriticalSectionBlock  )  [inline]
 

destructor automatically unlocks the critical section.

Definition at line 98 of file CriticalSectionBlock.H.

References leave_critical_section.


Member Function Documentation

bool CriticalSectionBlock::checkonce  )  [inline]
 

don't use.

Used internally for the synchronized() { } macro.

Definition at line 93 of file CriticalSectionBlock.H.


The documentation for this class was generated from the following file:
brickOS is released under the Mozilla Public License.
Original code copyright 1998-2002 by the authors.

Generated on Mon Feb 16 21:02:07 2004 for brickOS C++ by doxygen 1.3.5