SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions | Private Member Functions | Related Functions
RWCriticalSection Class Reference

Provides mutual exclusion for a critical section of code in which only one thread should execute at a time. More...

#include <rw/sync/RWCriticalSection.h>

Inheritance diagram for RWCriticalSection:
RWSynchObject

Public Types

typedef RWTLockGuard< RWCriticalSectionLockGuard
 
typedef RWTLockGuard< RWCriticalSectionReadLockGuard
 
typedef RWTUnlockGuard< RWCriticalSectionReadUnlockGuard
 
typedef RWTUnlockGuard< RWCriticalSectionUnlockGuard
 
typedef RWTLockGuard< RWCriticalSectionWriteLockGuard
 
typedef RWTUnlockGuard< RWCriticalSectionWriteUnlockGuard
 

Public Member Functions

 RWCriticalSection (RWStaticCtor)
 
 RWCriticalSection (RWCancellationState state=0)
 
 ~RWCriticalSection ()
 
void acquire ()
 
void acquireRead ()
 
void acquireWrite ()
 
RWCriticalSectionRepgetCriticalSectionRep () const
 
void release ()
 
- Public Member Functions inherited from RWSynchObject
void disableCancellation ()
 
void enableCancellation (RWCancellationState)
 
bool isCancellationEnabled () const
 
void setCancellation (RWCancellationState)
 

Private Member Functions

 RWCriticalSection (const RWCriticalSection &)
 
RWCriticalSectionoperator= (const RWCriticalSection &)
 

Related Functions

(Note that these are not member functions.)

typedef RW_MUTEX_T RWCriticalSectionRep
 

Additional Inherited Members

- Protected Member Functions inherited from RWSynchObject
 RWSynchObject (RWCancellationState state=0)
 
 RWSynchObject (RWStaticCtor)
 
 RWSynchObject (const RWSynchObject &second)
 
RWSynchObjectoperator= (const RWSynchObject &second)
 
void testCancellation ()
 

Detailed Description

An RWCriticalSection object is used to provide mutual exclusion for a critical section of code, i.e. code in which only one thread should execute at a time. This lock operates in a manner that is identical to RWMutexLock except that on certain platforms, for example Win32, it may yield superior performance when there is minimal contention for the lock.

Example
#include <rw/sync/RWCriticalSection.h>
void foo()
{
static RWCriticalSection outputCritsec(RW_STATIC_CTOR);
// ...
int i;
// ...
// Protect output from multiple threads
// using cout at the same time:
outputCritsec.acquire();
std::cout << "The value of i is " << i << std::endl;
outputCritsec.release();
// Same thing, this time using a guard:
{
RWCriticalSection::LockGuard lock(outputCritsec);
std::cout << "The value of i is " << i << std::endl;
}
}
See also
RWMutexLock

Member Typedef Documentation

Predefined type for compatible guard.

Predefined type for compatible guard.

Predefined type for compatible guard.

Predefined type for compatible guard.

Predefined type for compatible guard.

Predefined type for compatible guard.

Constructor & Destructor Documentation

RWCriticalSection::RWCriticalSection ( RWStaticCtor  )
inline

Constructs a static instance, but does no direct initialization. The RWCriticalSection is initialized on first use.

This constructor must be used only for static instances. Use of this constructor with automatically or dynamically allocated instances produces errors or other unpredictable behavior.

Static instances are zero initialized, which results in an RWCriticalSection with cancellation state of RW_CANCELLATION_DISABLED.

RWCriticalSection::RWCriticalSection ( RWCancellationState  state = 0)
inline

Creates and initializes the critical section. Possible exceptions include RWTHRInternalError.

RWCriticalSection::~RWCriticalSection ( )
inline

Default destructor.

RWCriticalSection::RWCriticalSection ( const RWCriticalSection )
private

Copy construction prohibited.

Member Function Documentation

void RWCriticalSection::acquire ( void  )
inline

Blocks until the critical section is available. Possible exceptions include RWCancellation and RWTHRInternalError.

In single-threaded builds, this method returns immediately. If the section is unavailable, a debug assertion will occur, or an RWTHRInternalError will be thrown.

void RWCriticalSection::acquireRead ( )
inline

Calls acquire(). Provided for compatibility with Read/Write locks. Possible exceptions include RWCancellation and RWTHRInternalError.

void RWCriticalSection::acquireWrite ( )
inline

Calls acquire(). Provided for compatibility with Read/Write locks. Possible exceptions include RWCancellation and RWTHRInternalError.

RWCriticalSectionRep * RWCriticalSection::getCriticalSectionRep ( ) const
inline

Provides access to the underlying platform-specific critical section implementation.

RWCriticalSection& RWCriticalSection::operator= ( const RWCriticalSection )
private

Assignment prohibited.

void RWCriticalSection::release ( void  )
inline

Releases the critical section. Possible exceptions include RWTHRInternalError.

Friends And Related Function Documentation

typedef RW_MUTEX_T RWCriticalSectionRep
related

Typedef for the internal critical section.

Condition:
This type is used for build configurations based on Win32 threads.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.