SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
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 (RWCancellationState state=0)
 
 RWCriticalSection (RWStaticCtor)
 
 ~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 Symbols

(Note that these are not member symbols.)

typedef RW_MUTEX_T RWCriticalSectionRep
 

Additional Inherited Members

- Protected Member Functions inherited from RWSynchObject
 RWSynchObject (const RWSynchObject &second)
 
 RWSynchObject (RWCancellationState state=0)
 
 RWSynchObject (RWStaticCtor)
 
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;
}
}
Provides mutual exclusion for a critical section of code in which only one thread should execute at a...
Definition RWCriticalSection.h:106
A guard that acquires its resource upon creation and releases it upon destruction.
Definition RWTLockGuard.h:61
@ RW_STATIC_CTOR
Definition trace/pkgdefs.h:87
See also
RWMutexLock

Member Typedef Documentation

◆ LockGuard

Predefined type for compatible guard.

◆ ReadLockGuard

Predefined type for compatible guard.

◆ ReadUnlockGuard

Predefined type for compatible guard.

◆ UnlockGuard

Predefined type for compatible guard.

◆ WriteLockGuard

Predefined type for compatible guard.

◆ WriteUnlockGuard

Predefined type for compatible guard.

Constructor & Destructor Documentation

◆ RWCriticalSection() [1/3]

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() [2/3]

RWCriticalSection::RWCriticalSection ( RWCancellationState state = 0)
inline

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

◆ ~RWCriticalSection()

RWCriticalSection::~RWCriticalSection ( )
inline

Default destructor.

◆ RWCriticalSection() [3/3]

RWCriticalSection::RWCriticalSection ( const RWCriticalSection & )
private

Copy construction prohibited.

Member Function Documentation

◆ acquire()

void RWCriticalSection::acquire ( )
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.

◆ acquireRead()

void RWCriticalSection::acquireRead ( )
inline

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

◆ acquireWrite()

void RWCriticalSection::acquireWrite ( )
inline

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

◆ getCriticalSectionRep()

RWCriticalSectionRep * RWCriticalSection::getCriticalSectionRep ( ) const
inline

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

◆ operator=()

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

Assignment prohibited.

◆ release()

void RWCriticalSection::release ( )
inline

Releases the critical section. Possible exceptions include RWTHRInternalError.

Friends And Related Symbol Documentation

◆ RWCriticalSectionRep

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 © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.