sem_destroy(3)

โœจ sem_destroy(3) โ€” Library Functions Manual โœจ

okay bestie, welcome to the sem_destroy(3) page from the Library Functions Manual ๐Ÿ’– this is the one where we say bye-bye to a semaphore, so let's get into it ๐Ÿซถ


๐Ÿ’ซ NAME

sem_destroy โ€” destroy an unnamed semaphore ๐ŸŒธ


๐Ÿ“š LIBRARY

POSIX threads library โ€” you're linking libpthread, so pop -lpthread on that compile line, bestie ๐ŸŽ€


๐Ÿฆ‹ SYNOPSIS

#include <semaphore.h>

int sem_destroy(sem_t *sem);

like... that's it. one function. one pointer. iconic behavior tbh ๐Ÿ‘‘


๐Ÿ’… DESCRIPTION

so sem_destroy() destroys the unnamed semaphore sitting at the address that sem points to. that's the whole job. she's done. she's retiring ๐Ÿ’€

now here's the tea: only a semaphore that was initialized by sem_init(3) should be destroyed using sem_destroy(). don't be out here destroying semaphores that came from somewhere else, that's not the vibe ๐Ÿ™ˆ

โš ๏ธ major warning, no cap: destroying a semaphore that other processes or threads are currently blocked on (like, blocked inside sem_wait(3)) produces undefined behavior. that is the scary kind of undefined. do not do it fr fr ๐Ÿ˜ญ

and also: using a semaphore that has already been destroyed produces undefined results, until the semaphore gets reinitialized using sem_init(3). so like, once she's gone she's GONE, no haunting the living unless you properly bring her back โœจ


๐Ÿ“ RETURN VALUE

  • on success: sem_destroy() returns 0 โ€” we love a clean win ๐Ÿ’–
  • on error: -1 is returned, and errno is set to indicate the error ๐Ÿ“

๐ŸŒธ ERRORS

  • EINVAL โ€” sem is not a valid semaphore. wrong vibe. invalid. rejected ๐Ÿ’€

๐ŸŒˆ ATTRIBUTES

for an explanation of the terms used in this section, check out attributes(7) (iykyk).

  • Interface: sem_destroy()
  • Attribute: Thread safety
  • Value: MT-Safe ๐Ÿ‘‘

โญ STANDARDS

POSIX.1-2008. periodt โœจ


๐ŸŽ€ HISTORY

POSIX.1-2001. she's been around a while, a true veteran ๐Ÿ’œ


๐Ÿงš NOTES

an unnamed semaphore should be destroyed with sem_destroy() before the memory that it lives in gets deallocated. yes, before. the order matters, bestie ๐Ÿซถ

โš ๏ธ failing to do this can result in resource leaks on some implementations. and leaks are not cute. leaks are the opposite of cute ๐Ÿ˜ญ

๐Ÿ”ฅ SEE ALSO


and that's the whole page, my love ๐Ÿ’– this was Linux man-pages 6.20, dated 2026-02-08, dropping sem_destroy(3) into the group chat. go free that semaphore (but like, responsibly, and after everyone's unblocked) ๐Ÿ˜Œโœจ๐Ÿ“๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms