Skip to content

sem_destroy()

Purpose

Destroys an unnamed POSIX semaphore initialized by sem_init().

#include <semaphore.h>

Prototype

int sem_destroy(sem_t *sem);

Parameters

  • sem: pointer to a POSIX semaphore.

Return Value

  • Success: returns 0.
  • Failure: returns -1 and sets errno.

Minimal Example

sem_destroy(&shared->items);
sem_destroy(&shared->spaces);

Common Pitfalls

  • Only destroy a semaphore when no process or thread may still use it.
  • Destroying a process-shared semaphore is usually the responsibility of the creator/owner.
  • sem_destroy() does not release shared memory.