---
name: "sema_init"
section: "9freebsd"
platform: "linux"
package: "freebsd-manpages"
version: "12.2-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/sema/9freebsd"
---

# sema\_init(9freebsd)

kernel counting semaphore

Part of [sema(9freebsd)](/sema/9freebsd.md).

## NAME

**sema**, **sema\_init**, **sema\_destroy**, **sema\_post**, **sema\_wait**, **sema\_timedwait**, **sema\_trywait**, **sema\_value** – kernel counting semaphore

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/lock.h\>**

**#include \<sys/sema.h\>**

*void*\

**sema\_init**(*struct sema \*sema*, *int value*, *const char \*description*);

*void*\

**sema\_destroy**(*struct sema \*sema*);

*void*\

**sema\_post**(*struct sema \*sema*);

*void*\

**sema\_wait**(*struct sema \*sema*);

*int*\

**sema\_timedwait**(*struct sema \*sema*, *int timo*);

*int*\

**sema\_trywait**(*struct sema \*sema*);

*int*\

**sema\_value**(*struct sema \*sema*);

## DESCRIPTION

Counting semaphores provide a mechanism for synchronizing access to a pool of resources. Unlike mutexes, semaphores do not have the concept of an owner, so they can also be useful in situations where one thread needs to acquire a resource, and another thread needs to release it. Each semaphore has an integer value associated with it. Posting (incrementing) always succeeds, but waiting (decrementing) can only successfully complete if the resulting value of the semaphore is greater than or equal to zero.

Semaphores should not be used where mutexes and condition variables will suffice. Semaphores are a more complex synchronization mechanism than mutexes and condition variables, and are not as efficient.

Semaphores are created with **sema\_init**(), where *sema* is a pointer to space for a *struct sema*, *value* is the initial value of the semaphore, and *description* is a pointer to a null-terminated character string that describes the semaphore. Semaphores are destroyed with **sema\_destroy**(). A semaphore is posted (incremented) with **sema\_post**(). A semaphore is waited on (decremented) with **sema\_wait**(), **sema\_timedwait**(), or **sema\_trywait**(). The *timo* argument to **sema\_timedwait**() specifies the minimum time in ticks to wait before returning with failure. **sema\_value**() is used to read the current value of the semaphore.

## RETURN VALUES

The **sema\_value**() function returns the current value of the semaphore.

If decrementing the semaphore would result in its value being negative, **sema\_trywait**() returns 0 to indicate failure. Otherwise, a non-zero value is returned to indicate success.

The **sema\_timedwait**() function returns 0 if waiting on the semaphore succeeded; otherwise a non-zero error code is returned.

## ERRORS

The **sema\_timedwait**() function will fail if:

- \[EWOULDBLOCK\]

  Timeout expired.

## SEE ALSO

[condvar(9)](/condvar/9.md), [locking(9)](/locking/9.md), [mtx\_pool(9)](/mtx_pool/9.md), [mutex(9)](/mutex/9.md), [rwlock(9)](/rwlock/9.md), [sx(9)](/sx/9.md)
