YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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.
 
virtual ~Semaphore ()
 Destructor.
 
void wait ()
 Decrement the counter, even if we must wait to do that.
 
bool waitWithTimeout (double timeoutInSeconds)
 Try to decrement the counter, even if we must wait - but don't wait forever.
 
bool check ()
 Decrement the counter, unless that would require waiting.
 
void post ()
 Increment the counter.
 

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 24 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 86 of file Semaphore.cpp.

◆ ~Semaphore()

Semaphore::~Semaphore ( )
virtual

Destructor.

Definition at line 91 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 106 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 111 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 96 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 101 of file Semaphore.cpp.


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