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

Semaphore synchronization primitive. More...

#include <semaphore.h>

Public Member Functions

 Semaphore (int resources)
 Default class constructor. More...
 
 Semaphore (const Semaphore &)=delete
 
 Semaphore (Semaphore &&semaphore)=delete
 
 ~Semaphore ()
 
Semaphoreoperator= (const Semaphore &)=delete
 
Semaphoreoperator= (Semaphore &&semaphore)=delete
 
int resources () const noexcept
 Get the semaphore resources counter. More...
 
bool TryLock ()
 Try to acquire semaphore without block. More...
 
bool TryLockFor (const Timespan &timespan)
 Try to acquire semaphore for the given timespan. More...
 
bool TryLockUntil (const UtcTimestamp &timestamp)
 Try to acquire semaphore until the given timestamp. More...
 
void Lock ()
 Acquire semaphore with block. More...
 
void Unlock ()
 Release semaphore. More...
 

Detailed Description

Semaphore synchronization primitive.

Semaphore synchronization primitive allows fixed count of threads to access some resource while other threads are waiting for it. When some thread unlocks the semaphore then one of waiting threads will lock it.

Thread-safe.

https://en.wikipedia.org/wiki/Semaphore_(programming)

Examples
threads_semaphore.cpp.

Definition at line 29 of file semaphore.h.

Constructor & Destructor Documentation

◆ Semaphore() [1/3]

CppCommon::Semaphore::Semaphore ( int  resources)
explicit

Default class constructor.

Parameters
resources- Semaphore resources counter

Definition at line 156 of file semaphore.cpp.

◆ Semaphore() [2/3]

CppCommon::Semaphore::Semaphore ( const Semaphore )
delete

◆ Semaphore() [3/3]

CppCommon::Semaphore::Semaphore ( Semaphore &&  semaphore)
delete

◆ ~Semaphore()

CppCommon::Semaphore::~Semaphore ( )

Definition at line 167 of file semaphore.cpp.

Member Function Documentation

◆ Lock()

void CppCommon::Semaphore::Lock ( )

Acquire semaphore with block.

Will block.

Definition at line 178 of file semaphore.cpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

Semaphore& CppCommon::Semaphore::operator= ( Semaphore &&  semaphore)
delete

◆ resources()

int CppCommon::Semaphore::resources ( ) const
noexcept

Get the semaphore resources counter.

Definition at line 173 of file semaphore.cpp.

◆ TryLock()

bool CppCommon::Semaphore::TryLock ( )

Try to acquire semaphore without block.

Will not block.

Returns
'true' if the semaphore was successfully acquired, 'false' if the semaphore is busy
Examples
threads_semaphore.cpp.

Definition at line 175 of file semaphore.cpp.

◆ TryLockFor()

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

Try to acquire semaphore for the given timespan.

Will block for the given timespan in the worst case.

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

Definition at line 176 of file semaphore.cpp.

◆ TryLockUntil()

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

Try to acquire semaphore until the given timestamp.

Will block until the given timestamp in the worst case.

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

Definition at line 70 of file semaphore.h.

◆ Unlock()

void CppCommon::Semaphore::Unlock ( )

Release semaphore.

Will not block.

Examples
threads_semaphore.cpp.

Definition at line 179 of file semaphore.cpp.


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