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

Named semaphore synchronization primitive. More...

#include <named_semaphore.h>

Public Member Functions

 NamedSemaphore (const std::string &name, int resources)
 Default class constructor. More...
 
 NamedSemaphore (const NamedSemaphore &)=delete
 
 NamedSemaphore (NamedSemaphore &&semaphore)=delete
 
 ~NamedSemaphore ()
 
NamedSemaphoreoperator= (const NamedSemaphore &)=delete
 
NamedSemaphoreoperator= (NamedSemaphore &&semaphore)=delete
 
const std::string & name () const
 Get the semaphore name. More...
 
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

Named semaphore synchronization primitive.

Named semaphore behaves as a simple semaphore but could be shared between processes on the same machine.

Thread-safe.

See also
Semaphore
Examples
threads_named_semaphore.cpp.

Definition at line 29 of file named_semaphore.h.

Constructor & Destructor Documentation

◆ NamedSemaphore() [1/3]

CppCommon::NamedSemaphore::NamedSemaphore ( const std::string &  name,
int  resources 
)
explicit

Default class constructor.

Parameters
name- Semaphore name
resources- Semaphore resources counter

Definition at line 184 of file named_semaphore.cpp.

◆ NamedSemaphore() [2/3]

CppCommon::NamedSemaphore::NamedSemaphore ( const NamedSemaphore )
delete

◆ NamedSemaphore() [3/3]

CppCommon::NamedSemaphore::NamedSemaphore ( NamedSemaphore &&  semaphore)
delete

◆ ~NamedSemaphore()

CppCommon::NamedSemaphore::~NamedSemaphore ( )

Definition at line 195 of file named_semaphore.cpp.

Member Function Documentation

◆ Lock()

void CppCommon::NamedSemaphore::Lock ( )

Acquire semaphore with block.

Will block.

Definition at line 207 of file named_semaphore.cpp.

◆ name()

const std::string & CppCommon::NamedSemaphore::name ( ) const

Get the semaphore name.

Definition at line 201 of file named_semaphore.cpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ resources()

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

Get the semaphore resources counter.

Definition at line 202 of file named_semaphore.cpp.

◆ TryLock()

bool CppCommon::NamedSemaphore::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_named_semaphore.cpp.

Definition at line 204 of file named_semaphore.cpp.

◆ TryLockFor()

bool CppCommon::NamedSemaphore::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 205 of file named_semaphore.cpp.

◆ TryLockUntil()

bool CppCommon::NamedSemaphore::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 73 of file named_semaphore.h.

◆ Unlock()

void CppCommon::NamedSemaphore::Unlock ( )

Release semaphore.

Will not block.

Examples
threads_named_semaphore.cpp.

Definition at line 208 of file named_semaphore.cpp.


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