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

Unique acquire from a Semaphore. More...

#include <UniqueAcquire.hpp>

Collaboration diagram for samchon::library::UniqueAcquire:

Public Member Functions

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

Private Attributes

Semaphoresemaphore
 Managed semaphore. More...
 
bool isLocked
 Whether the semaphore was acquired by the UniqueLock. More...
 

Detailed Description

Unique acquire from a Semaphore.

A UniqueAcquire is an object manages a Semaphore with unique ownership in both states.

  • acquired
  • released

On construction (or by move-assigning to it), the object acquires a semaphore object, for whose acquiring and releasing operations becomes responsible.

This class guarantees a released status on destruction (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 UniqueAcquire 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 UniqueAcquire that manages it.

The UniqueAcquire is movable, but not copyable.

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

Definition at line 41 of file UniqueAcquire.hpp.

Constructor & Destructor Documentation

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

Construct from semaphore.

Parameters
semaphoreSemaphore to manage
doLockWhether to lock directly or not

Definition at line 9 of file UniqueAcquire.cpp.

References samchon::library::Semaphore::acquire(), isLocked, and semaphore.

Here is the call graph for this function:

samchon::library::UniqueAcquire::UniqueAcquire ( const UniqueAcquire )
delete

Prohibited Copy Constructor.

UniqueAcquire can't be copied. Use pointer, reference instead.

If what you want is UniqueAcquire(s) references each other and unlock when all related UniqueAcquire objects are destructed, SharedAcquire is the best way.

Parameters
objTried object to copy.
UniqueAcquire::UniqueAcquire ( UniqueAcquire &&  obj)

Move Constructor.

Parameters
Anobject to move

Definition at line 17 of file UniqueAcquire.cpp.

References isLocked, and semaphore.

UniqueAcquire::~UniqueAcquire ( )

Default Destructor.

If read lock has done by the UniqueLock, unlock it

Definition at line 27 of file UniqueAcquire.cpp.

References isLocked, samchon::library::Semaphore::release(), and semaphore.

Here is the call graph for this function:

Member Function Documentation

void UniqueAcquire::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 36 of file UniqueAcquire.cpp.

References samchon::library::Semaphore::acquire(), isLocked, and semaphore.

Here is the call graph for this function:

void UniqueAcquire::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 44 of file UniqueAcquire.cpp.

References isLocked, samchon::library::Semaphore::release(), and semaphore.

Here is the call graph for this function:

Member Data Documentation

Semaphore* samchon::library::UniqueAcquire::semaphore
private

Managed semaphore.

Definition at line 47 of file UniqueAcquire.hpp.

Referenced by acquire(), release(), UniqueAcquire(), and ~UniqueAcquire().

bool samchon::library::UniqueAcquire::isLocked
private

Whether the semaphore was acquired by the UniqueLock.

Definition at line 52 of file UniqueAcquire.hpp.

Referenced by acquire(), release(), UniqueAcquire(), and ~UniqueAcquire().


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