---
name: "Semaphore"
section: "3cc"
platform: "linux"
package: "libcommoncpp2-dev"
version: "1.0.13-5"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/Semaphore"
---

# Semaphore(3cc)

A semaphore is generally used as a synchronization object between multiple threads or to protect a limited and finite resource such as a memory or thread pool. Semaphore counter for thread synchronization.

## NAME

Semaphore - A semaphore is generally used as a synchronization object between multiple threads or to protect a limited and finite resource such as a memory or thread pool. Semaphore counter for thread synchronization.

## SYNOPSIS

`#include <thread.h>`

### Public Methods

**Semaphore** (size\_t resource=0)\
 *The initial value of the semaphore can be specified.* virtual **~Semaphore** ()\
 *Destroying a semaphore also removes any system resources associated with it.* void **wait** (void)\
 *Wait is used to keep a thread held until the semaphore counter is greater than 0.* bool **tryWait** (void)\
 *TryWait is a non-blocking variant of Wait.* void **post** (void)\
 *Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0.* int **getValue** (void)\
 *Get the current value of a semaphore.*

## DETAILED DESCRIPTION

A semaphore is generally used as a synchronization object between multiple threads or to protect a limited and finite resource such as a memory or thread pool. Semaphore counter for thread synchronization.

The semaphore has a counter which only permits access by one or more threads when the value of the semaphore is non-zero. Each access reduces the current value of the semaphore by 1. One or more threads can wait on a semaphore until it is no longer 0, and hence the semaphore can be used as a simple thread synchronization object to enable one thread to pause others until the thread is ready or has provided data for them. Semaphores are typically used as a counter for protecting or limiting concurrent access to a given resource, such as to permitting at most 'x' number of threads to use resource 'y', for example.

**Author:**  David Sugar \<dyfet@ostel.com\>

## CONSTRUCTOR & DESTRUCTOR DOCUMENTATION

### Semaphore::Semaphore (size\_t resource = 0)

The initial value of the semaphore can be specified.

An initial value is often used When used to lock a finite resource or to  specify the maximum number of thread instances that can access a  specified resource.

**Parameters:**

- *resource*

  *specify initial resource count or 0 default.*

### virtual Semaphore::~Semaphore ()` [virtual]`

*Destroying a semaphore also removes any system resources associated with it.*

*If a semaphore has threads currently waiting on it, those threads will all continue when a semaphore is destroyed.*

## MEMBER FUNCTION DOCUMENTATION

### int Semaphore::getValue (void)

*Get the current value of a semaphore.*

**Returns:**  *current value.*

### void Semaphore::post (void)

*Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0.*

*Interestingly, there is no support to increment a semaphore by any value greater than 1 to release multiple waiting threads in either pthread or the win32 API. Hence, if one wants to release a semaphore to enable multiple threads to execute, one must perform multiple post operations.*

**See also:**  **wait**

### bool Semaphore::tryWait (void)

*TryWait is a non-blocking variant of Wait.*

*If the semaphore counter is greater than 0, then the thread is accepted and the semaphore counter is decreased. If the semaphore counter is 0 TryWait returns immediately with false.*

**Returns:**  *true if thread is accepted otherwise false*

**See also:**  **wait** *,* **post**

### void Semaphore::wait (void)

*Wait is used to keep a thread held until the semaphore counter is greater than 0.*

*If the current thread is held, then another thread must increment the semaphore. Once the thread is accepted,  the semaphore is automatically decremented, and the thread  continues execution.*

*The pthread semaphore object does not support a timed 'wait', and hence to maintain consistancy, neither the posix nor win32 source trees support 'timed' semaphore objects.*

**See also:**  **post**

## AUTHOR

*Generated automatically by Doxygen for CommonC++ from the source code.*
