PLCnext API Documentation 23.6.0.37
Public Member Functions | List of all members
Arp::System::Commons::Threading::Semaphore Class Reference

Implementation of named or unnamed semaphore used to synchronize processes and threads. More...

#include <Semaphore.hpp>

Public Member Functions

 Semaphore (size_t initialValue)
 Constructs an unnamed Semaphore, i.e. local, instance. More...
 
 Semaphore (const String &name, size_t initialValue)
 Constructs a named Semaphore instance. More...
 
 Semaphore (const String &name)
 Opens a named Semaphore instance. More...
 
virtual ~Semaphore (void)
 Destructs this instance and frees all resources.
 
void Post (void)
 Increments the internal counter. More...
 
bool Wait (size_t timeout=0)
 Tries to decrements the internal counter. More...
 
bool TryWait (void)
 Like Wait but returns immediately if the semaphore could not be decremented. More...
 
size_t GetValue (void)
 Returns the current value of the internal counter. More...
 
String GetName (void)
 Returns the name of a named semaphore. More...
 

Detailed Description

Implementation of named or unnamed semaphore used to synchronize processes and threads.

Semaphores are mainly integer based counters. Whenever one of the wait-methods are called on an instance, the internal counter is decremeneted by one. If the current value of the counter on an invocation of Wait is zero, than the call blocks until another thread calls Post on the same semaphore instance.

Constructor & Destructor Documentation

◆ Semaphore() [1/3]

Arp::System::Commons::Threading::Semaphore::Semaphore ( size_t  initialValue)
explicit

Constructs an unnamed Semaphore, i.e. local, instance.

Local semaphores can only be used inside the same memory space, i.e. from tasks inside the same process.

Parameters
initialValueThe initial value of the semaphore.
Exceptions
Arp::System::Commons::InvalidOperationExceptionif the semaphore could not be created.

◆ Semaphore() [2/3]

Arp::System::Commons::Threading::Semaphore::Semaphore ( const String name,
size_t  initialValue 
)

Constructs a named Semaphore instance.

Named semaphores can be used for synchronization purposes between processes not only between threads of the same process.

Parameters
nameA name identifying the semaphore in the root file s
initialValueThe initial value of the semaphore.
Exceptions
Arp::System::Commons::InvalidOperationExceptionif the semaphore could not be created.
Arp::System::Commons::Io::AlreadyExistsExceptionif a semaphore with the same name already exists.

◆ Semaphore() [3/3]

Arp::System::Commons::Threading::Semaphore::Semaphore ( const String name)
explicit

Opens a named Semaphore instance.

Tries to open the semaphore identified by name. If no such semaphore exists, an exception is thrown.

Parameters
nameName of the semaphore to open.
Exceptions
Arp::System::Commons::InvalidOperationExceptionif the semaphore could not be created.

Member Function Documentation

◆ GetName()

String Arp::System::Commons::Threading::Semaphore::GetName ( void  )

Returns the name of a named semaphore.

Returns
Name if this instance is a named semaphore, otherwise an emptry string is returned.

◆ GetValue()

size_t Arp::System::Commons::Threading::Semaphore::GetValue ( void  )

Returns the current value of the internal counter.

Returns
Current value of internal counter.
Exceptions
Arp::Exceptionwith detailed error message in case operation fails.

◆ Post()

void Arp::System::Commons::Threading::Semaphore::Post ( void  )

Increments the internal counter.

Exceptions
Arp::Exceptionwith detailed error message in case operation fails.

◆ TryWait()

bool Arp::System::Commons::Threading::Semaphore::TryWait ( void  )

Like Wait but returns immediately if the semaphore could not be decremented.

Returns
True if semaphore is decremented, otherwise false is returned.
Exceptions
Arp::Exceptionwith detailed error message in case operation fails.

◆ Wait()

bool Arp::System::Commons::Threading::Semaphore::Wait ( size_t  timeout = 0)

Tries to decrements the internal counter.

If the counter is zero at invocation, the call blocks until the counter is incremented by a call to Post.

Returns
True if the semaphore was successfully decremented, otherwise false.
Exceptions
Arp::Exceptionwith detailed error message in case operation fails.

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