YARP
Yet Another Robot Platform
yarp::os::Semaphore Class Reference

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore::Semaphore ( unsigned int  initialCount = 1)

Constructor.

Sets the initial value of the counter.

Parameters
initialCountinitial value of the counter

Definition at line 89 of file Semaphore.cpp.

◆ ~Semaphore()

Semaphore::~Semaphore ( )
virtual

Destructor.

Definition at line 94 of file Semaphore.cpp.

Member Function Documentation

◆ check()

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.

Returns
true if the counter was decremented

Definition at line 109 of file Semaphore.cpp.

◆ post()

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.

◆ wait()

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.

◆ waitWithTimeout()

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.

Parameters
timeoutInSecondsthe maximum length of time to wait, in seconds (may be fractional).
Returns
true if the counter was decremented, false if a timeout occurred.

Definition at line 104 of file Semaphore.cpp.


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