Samchon Framework for CPP  1.0.0
samchon::library::SharedAcquire Class Reference

Shared acquire from a Semaphore. More...

#include <SharedAcquire.hpp>

Collaboration diagram for samchon::library::SharedAcquire:

Public Member Functions

 SharedAcquire (Semaphore &, bool=true)
 Construct from Semaphore. More...
 
 SharedAcquire (const SharedAcquire &)
 Copy Constructor. More...
 
 SharedAcquire (SharedAcquire &&)
 Move Constructor. More...
 
 ~SharedAcquire ()
 Default Destructor. More...
 
void acquire ()
 Acquire admission. More...
 
void release ()
 Release an admission. More...
 

Private Attributes

Semaphoresemaphore
 Managed Semaphore. More...
 
std::atomic< size_t > * reference
 Referencing count sharing same Semaphore. More...
 
std::atomic< bool > * isLocked
 Whether the mutex was locked by SharedAcquire. More...
 

Detailed Description

Shared acquire from a Semaphore.

A SharedAcquire is an object manages a Semaphore with shared ownership in both states.

  • acquired
  • released

On default construction, the object acquires a Semaphore object, for whose acquiring and releasing operations becomes responsible. When copy constructions, responsibilities of acquiring and releasing Semaphore are shared with copied with those SharedAcquire objects.

The class shared_lock is a general-purpose shared mutex ownership wrapper allowing deferred locking, timed locking and transfer of lock ownership. Locking a shared_lock locks the associated shared mutex in shared mode (to lock it in exclusive mode, std::unique_lock can be used)

This class guarantees a released status on destruction of all shared objects (even if not called explicitly). Therefore it is especially useful as an object with automatic duration, as it guarantees the Semaphore object is properly released in case an exception is thrown.

Referenced comments of std::unique_lock

library_critical_section.png
Note

Though, that the SharedAcquire object does not manage the lifetime of the Semaphore object in any way: the duration of the Semaphore object shall extend at least until the destruction of the SharedAcquire that manages it.

See also
library::UniqueAcquire
samchon::library
Author
Jeongho Nam http://samchon.org

Definition at line 51 of file SharedAcquire.hpp.

Constructor & Destructor Documentation

SharedAcquire::SharedAcquire ( Semaphore semaphore,
bool  doLock = true 
)

Construct from Semaphore.

Parameters
semaphoreSemaphore to manage
doLockWhether to lock directly or not

Definition at line 12 of file SharedAcquire.cpp.

References samchon::library::Semaphore::acquire().

Here is the call graph for this function:

SharedAcquire::SharedAcquire ( const SharedAcquire obj)

Copy Constructor.

Definition at line 22 of file SharedAcquire.cpp.

References isLocked, reference, and semaphore.

SharedAcquire::SharedAcquire ( SharedAcquire &&  obj)

Move Constructor.

Definition at line 30 of file SharedAcquire.cpp.

SharedAcquire::~SharedAcquire ( )

Default Destructor.

Definition at line 43 of file SharedAcquire.cpp.

Member Function Documentation

void SharedAcquire::acquire ( )

Acquire admission.

Acquires an admission and increases count of admission by 1.

If the count is over permitted size, wait until other admissions to be released.

  • Lock on mutex

Definition at line 58 of file SharedAcquire.cpp.

void SharedAcquire::release ( )

Release an admission.

Releases an admission what you've acquired. If the admission count was over the limited size, unlock the mutex.

Definition at line 66 of file SharedAcquire.cpp.

Member Data Documentation

Semaphore* samchon::library::SharedAcquire::semaphore
private

Managed Semaphore.

Definition at line 57 of file SharedAcquire.hpp.

Referenced by SharedAcquire().

std::atomic<size_t>* samchon::library::SharedAcquire::reference
private

Referencing count sharing same Semaphore.

Definition at line 62 of file SharedAcquire.hpp.

Referenced by SharedAcquire().

std::atomic<bool>* samchon::library::SharedAcquire::isLocked
private

Whether the mutex was locked by SharedAcquire.

Definition at line 67 of file SharedAcquire.hpp.

Referenced by SharedAcquire().


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