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

A semaphore. More...

#include <Semaphore.hpp>

Public Member Functions

 Semaphore (size_t capacity=2)
 
void setCapacity (size_t val)
 Set size. More...
 
auto capacity () const -> size_t
 Get size. More...
 
auto acquired () const -> size_t
 Get acquired size. More...
 
void acquire ()
 Acquire admission. More...
 
auto tryAcquire () -> bool
 Try to acquire admission. More...
 
void release ()
 Release an admission. More...
 

Private Attributes

size_t capacity_
 The size. More...
 
size_t acquired_
 Acquired count. More...
 
std::mutex mtx
 Locker. More...
 

Detailed Description

A semaphore.

In computer science, particularly in operating systems, a semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a concurrent system such as a multiprogramming operating system.

A trivial semaphore is a plain variable that is changed (for example, incremented or decremented, or toggled) depending on programmer-defined conditions. The variable is then used as a condition to control access to some system resource.

A useful way to think of a semaphore as used in the real-world systems is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and, if necessary, wait until a unit of the resource becomes available. Semaphores are a useful tool in the prevention of race conditions; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores

  • Categorized in binary semaphore

Referenced Wediapedia

Class Diagram
Note

Of course, semaphore is already defined in linux C and MFC in Window. But it is dependent on each operating system, so that cannot be compiled in another OS with those semaphores. There's not a class like semaphore in STL yet. It's the reason why Semaphore is provided.

As that reason, if STL supports the semaphore in near future, the Semaphore can be deprecated.

Library - Critical Section

Author
Jeongho Nam http://samchon.org

Definition at line 47 of file Semaphore.hpp.

Constructor & Destructor Documentation

samchon::library::Semaphore::Semaphore ( size_t  capacity = 2)
inline

Constructor.

Parameters
sizeThe size of the semaphore to permit

Definition at line 78 of file Semaphore.hpp.

References capacity().

Here is the call graph for this function:

Member Function Documentation

void samchon::library::Semaphore::setCapacity ( size_t  val)
inline

Set size.

Set permitted size of the semaphore.

Definition at line 88 of file Semaphore.hpp.

auto samchon::library::Semaphore::capacity ( ) const -> size_t
inline

Get size.

Returns size which means the permitted count of the semaphore

Returns
The size of semaphore

Definition at line 104 of file Semaphore.hpp.

References capacity_.

Referenced by Semaphore().

Here is the caller graph for this function:

auto samchon::library::Semaphore::acquired ( ) const -> size_t
inline

Get acquired size.

Definition at line 112 of file Semaphore.hpp.

References acquired_.

void samchon::library::Semaphore::acquire ( )
inline

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 128 of file Semaphore.hpp.

Referenced by samchon::library::UniqueAcquire::acquire(), and samchon::library::SharedAcquire::acquire().

Here is the caller graph for this function:

auto samchon::library::Semaphore::tryAcquire ( ) -> bool
inline

Try to acquire admission.

If admission count is below the permitted size, acquire admission and increase the count by 1 and return true which means succeded to get admission.

  • If the count is matched to the permitted size, lock the mutex

Else, do not acquire admission and return false which means failed to get admmission.

Returns
Whether succeded to acquire an admission or not

Definition at line 152 of file Semaphore.hpp.

void samchon::library::Semaphore::release ( )
inline

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 174 of file Semaphore.hpp.

Referenced by samchon::library::UniqueAcquire::release(), samchon::library::SharedAcquire::release(), samchon::library::SharedAcquire::~SharedAcquire(), and samchon::library::UniqueAcquire::~UniqueAcquire().

Here is the caller graph for this function:

Member Data Documentation

size_t samchon::library::Semaphore::capacity_
private

The size.

Permitted size of the semaphore

Definition at line 54 of file Semaphore.hpp.

Referenced by capacity().

size_t samchon::library::Semaphore::acquired_
private

Acquired count.

Definition at line 62 of file Semaphore.hpp.

Referenced by acquired().

std::mutex samchon::library::Semaphore::mtx
private

Locker.

Manages lock and unlock of the semaphore

Definition at line 70 of file Semaphore.hpp.


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