Panda3D
Public Member Functions | List of all members
Semaphore Class Reference

A classic semaphore synchronization primitive. More...

#include "psemaphore.h"

Public Member Functions

 Semaphore (int initial_count=1)
 
void acquire ()
 Decrements the internal count. More...
 
int get_count () const
 Returns the current semaphore count. More...
 
void output (ostream &out) const
 
int release ()
 Increments the semaphore's internal count. More...
 
bool try_acquire ()
 If the semaphore can be acquired without blocking, does so and returns true. More...
 

Detailed Description

A classic semaphore synchronization primitive.

A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release().

Definition at line 34 of file psemaphore.h.

Member Function Documentation

◆ acquire()

void Semaphore::acquire ( )
inline

Decrements the internal count.

If the count was already at zero, blocks until the count is nonzero, then decrements it.

Definition at line 69 of file psemaphore.I.

References try_acquire(), and ConditionVarDirect::wait().

◆ get_count()

int Semaphore::get_count ( ) const
inline

Returns the current semaphore count.

Note that this call is not thread-safe (the count may change at any time).

Definition at line 122 of file psemaphore.I.

Referenced by release().

◆ release()

int Semaphore::release ( )
inline

Increments the semaphore's internal count.

This may wake up another thread blocked on acquire().

Returns the count of the semaphore upon release.

Definition at line 106 of file psemaphore.I.

References get_count(), and ConditionVarDirect::notify().

Referenced by try_acquire().

◆ try_acquire()

bool Semaphore::try_acquire ( )
inline

If the semaphore can be acquired without blocking, does so and returns true.

Otherwise, returns false.

Definition at line 86 of file psemaphore.I.

References release().

Referenced by acquire().


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