A class for thread synchronization and mutual exclusion. More...
#include <yarp/os/Semaphore.h>
Classes | |
class | Private |
Public Member Functions | |
Semaphore (unsigned int initialCount=1) | |
Constructor. More... | |
virtual | ~Semaphore () |
Destructor. More... | |
void | wait () |
Decrement the counter, even if we must wait to do that. More... | |
bool | waitWithTimeout (double timeoutInSeconds) |
Try to decrement the counter, even if we must wait - but don't wait forever. More... | |
bool | check () |
Decrement the counter, unless that would require waiting. More... | |
void | post () |
Increment the counter. More... | |
A class for thread synchronization and mutual exclusion.
A semaphore has an internal counter. Multiple threads can safely increment or decrement that counter. If one thread attempts to decrement the counter below zero, it must wait for another thread to first increment it. This is a useful primitive for regulating thread interaction.
Definition at line 28 of file Semaphore.h.
Semaphore::Semaphore | ( | unsigned int | initialCount = 1 | ) |
Constructor.
Sets the initial value of the counter.
initialCount | initial value of the counter |
Definition at line 89 of file Semaphore.cpp.
|
virtual |
Destructor.
Definition at line 94 of file Semaphore.cpp.
bool Semaphore::check | ( | ) |
Decrement the counter, unless that would require waiting.
If the counter would decrement below zero, this method simply returns without doing anything.
Definition at line 109 of file Semaphore.cpp.
void Semaphore::post | ( | ) |
Increment the counter.
If another thread is waiting to decrement the counter, it is woken up.
Definition at line 114 of file Semaphore.cpp.
void Semaphore::wait | ( | ) |
Decrement the counter, even if we must wait to do that.
If the counter would decrement below zero, the calling thread must stop and wait for another thread to call Semaphore::post on this semaphore.
Definition at line 99 of file Semaphore.cpp.
bool Semaphore::waitWithTimeout | ( | double | timeoutInSeconds | ) |
Try to decrement the counter, even if we must wait - but don't wait forever.
This method wiill wait for at most timeoutInSeconds seconds (this can be fractional). If the counter has not been decremented within that interval, the method will return false.
timeoutInSeconds | the maximum length of time to wait, in seconds (may be fractional). |
Definition at line 104 of file Semaphore.cpp.