---
name: "sem_destroy"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/sem_destroy"
---

# sem\_destroy(3)

destroy an unnamed semaphore

## NAME

sem\_destroy - destroy an unnamed semaphore

## LIBRARY

POSIX threads library (*libpthread*, *-lpthread*)

## SYNOPSIS

```
#include <semaphore.h>
```

**int sem\_destroy(sem\_t \****sem***);**

## DESCRIPTION

**sem\_destroy**() destroys the unnamed semaphore at the address pointed to by *sem*.

Only a semaphore that has been initialized by [**sem\_init**(3)](/sem_init/3.md) should be destroyed using **sem\_destroy**().

Destroying a semaphore that other processes or threads are currently blocked on (in [**sem\_wait**(3)](/sem_wait/3.md)) produces undefined behavior.

Using a semaphore that has been destroyed produces undefined results, until the semaphore has been reinitialized using [**sem\_init**(3)](/sem_init/3.md).

## RETURN VALUE

**sem\_destroy**() returns 0 on success; on error, -1 is returned, and *errno* is set to indicate the error.

## ERRORS

- **EINVAL**

  *sem* is not a valid semaphore.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **sem\_destroy**() | Thread safety | MT-Safe |

## STANDARDS

POSIX.1-2008.

## HISTORY

POSIX.1-2001.

## NOTES

An unnamed semaphore should be destroyed with **sem\_destroy**() before the memory in which it is located is deallocated. Failure to do this can result in resource leaks on some implementations.

## SEE ALSO

[**sem\_init**(3)](/sem_init/3.md), [**sem\_post**(3)](/sem_post/3.md), [**sem\_wait**(3)](/sem_wait/3.md), [**sem\_overview**(7)](/sem_overview/7.md)
