PLCnext API Documentation
22.9.0.33
|
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. More... | |
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... | |
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.
|
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.
initialValue | The initial value of the semaphore. |
Arp::System::Commons::InvalidOperationException | if the semaphore could not be created. |
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.
name | A name identifying the semaphore in the root file s |
initialValue | The initial value of the semaphore. |
Arp::System::Commons::InvalidOperationException | if the semaphore could not be created. |
Arp::System::Commons::Io::AlreadyExistsException | if a semaphore with the same name already exists. |
|
explicit |
Opens a named Semaphore instance.
Tries to open the semaphore identified by name. If no such semaphore exists, an exception is thrown.
name | Name of the semaphore to open. |
Arp::System::Commons::InvalidOperationException | if the semaphore could not be created. |
|
virtual |
Destructs this instance and frees all resources.
String Arp::System::Commons::Threading::Semaphore::GetName | ( | void | ) |
Returns the name of a named semaphore.
size_t Arp::System::Commons::Threading::Semaphore::GetValue | ( | void | ) |
Returns the current value of the internal counter.
Arp::Exception | with detailed error message in case operation fails. |
void Arp::System::Commons::Threading::Semaphore::Post | ( | void | ) |
Increments the internal counter.
Arp::Exception | with detailed error message in case operation fails. |
bool Arp::System::Commons::Threading::Semaphore::TryWait | ( | void | ) |
Like Wait but returns immediately if the semaphore could not be decremented.
Arp::Exception | with detailed error message in case operation fails. |
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.
Arp::Exception | with detailed error message in case operation fails. |