CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Friends | List of all members
CppCommon::CriticalSection Class Reference

Critical section synchronization primitive. More...

#include <critical_section.h>

Public Member Functions

 CriticalSection ()
 
 CriticalSection (const CriticalSection &)=delete
 
 CriticalSection (CriticalSection &&cs)=delete
 
 ~CriticalSection ()
 
CriticalSectionoperator= (const CriticalSection &)=delete
 
CriticalSectionoperator= (CriticalSection &&cs)=delete
 
bool TryLock ()
 Try to acquire critical section without block. More...
 
bool TryLockFor (const Timespan &timespan)
 Try to acquire critical section for the given timespan. More...
 
bool TryLockUntil (const UtcTimestamp &timestamp)
 Try to acquire critical section until the given timestamp. More...
 
void Lock ()
 Acquire critical section with block. More...
 
void Unlock ()
 Release critical section. More...
 

Friends

class ConditionVariable
 

Detailed Description

Critical section synchronization primitive.

Critical sections prevents code fragments from access by multiple threads simultaneously. Only one thread can access the code inside the critical section. Other threads must wait for the lock! Critical sections are usually more lightweight than mutexes and don't enter kernel mode.

Thread-safe.

https://en.wikipedia.org/wiki/Critical_section

Examples
threads_condition_variable.cpp, threads_critical_section.cpp, and threads_thread.cpp.

Definition at line 29 of file critical_section.h.

Constructor & Destructor Documentation

◆ CriticalSection() [1/3]

CppCommon::CriticalSection::CriticalSection ( )

Definition at line 110 of file critical_section.cpp.

◆ CriticalSection() [2/3]

CppCommon::CriticalSection::CriticalSection ( const CriticalSection )
delete

◆ CriticalSection() [3/3]

CppCommon::CriticalSection::CriticalSection ( CriticalSection &&  cs)
delete

◆ ~CriticalSection()

CppCommon::CriticalSection::~CriticalSection ( )

Definition at line 121 of file critical_section.cpp.

Member Function Documentation

◆ Lock()

void CppCommon::CriticalSection::Lock ( )

Acquire critical section with block.

Will block.

Examples
threads_condition_variable.cpp.

Definition at line 155 of file critical_section.cpp.

◆ operator=() [1/2]

CriticalSection& CppCommon::CriticalSection::operator= ( const CriticalSection )
delete

◆ operator=() [2/2]

CriticalSection& CppCommon::CriticalSection::operator= ( CriticalSection &&  cs)
delete

◆ TryLock()

bool CppCommon::CriticalSection::TryLock ( )

Try to acquire critical section without block.

Will not block.

Returns
'true' if the critical section was successfully acquired, 'false' if the critical section is busy

Definition at line 129 of file critical_section.cpp.

◆ TryLockFor()

bool CppCommon::CriticalSection::TryLockFor ( const Timespan timespan)

Try to acquire critical section for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the critical section
Returns
'true' if the critical section was successfully acquired, 'false' if the critical section is busy

Definition at line 131 of file critical_section.cpp.

◆ TryLockUntil()

bool CppCommon::CriticalSection::TryLockUntil ( const UtcTimestamp timestamp)
inline

Try to acquire critical section until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the critical section
Returns
'true' if the critical section was successfully acquired, 'false' if the critical section is busy

Definition at line 65 of file critical_section.h.

◆ Unlock()

void CppCommon::CriticalSection::Unlock ( )

Release critical section.

Will not block.

Examples
threads_condition_variable.cpp.

Definition at line 156 of file critical_section.cpp.

Friends And Related Function Documentation

◆ ConditionVariable

friend class ConditionVariable
friend

Definition at line 31 of file critical_section.h.


The documentation for this class was generated from the following files: