pthread_spin_lock(3)
β¨ pthread_spin_lock β bestie, let's talk spin locks β¨
okay so this is the pthread_spin_lock page from the Linux Library Functions Manual (that's section 3, the library-call girlies π
), and it's all about three lil functions that lock and unlock a spin lock: pthread_spin_lock, pthread_spin_trylock, and pthread_spin_unlock. no cap. let's get into it π¦
πΈ LIBRARY πΈ
it lives in the POSIX threads library, which is libpthread, so you link it with -lpthread. that's the whole vibe π
β¨ SYNOPSIS β¨
#include <pthread.h>
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);
int pthread_spin_unlock(pthread_spinlock_t *lock);
π Feature Test Macro Requirements for glibc π
(and if you're like "what even is a feature test macro", go look at feature_test_macros(7), she explains everything π«Ά)
pthread_spin_lock(),pthread_spin_trylock():_POSIX_C_SOURCE >= 200112L
π DESCRIPTION π
okay so pthread_spin_lock() locks the spin lock that lock points at. if that spin lock is unlocked right now, the calling thread grabs it immediately, no waiting, instant slay β¨. but if some other thread is already holding it, your thread just spins β like, literally keeps testing the lock over and over β until it frees up, and then your thread acquires it. it's giving "are we there yet" energy, but make it a CPU loop π
warning bestie: callingpthread_spin_lock()on a lock that the caller already holds, OR on a lock that was never initialized withpthread_spin_init(3), is undefined behavior. that means the universe can do whatever it wants. do not test it. π
pthread_spin_trylock() is like pthread_spin_lock() except β and this is the iconic part β if the spin lock pointed to by lock is currently locked, it does NOT spin. it just returns immediately with the error EBUSY. one and done, no waiting around. periodt π
pthread_spin_unlock() unlocks the spin lock that lock refers to. and if any threads are out there spinning on that lock, one of them (just one, bestie) will then acquire it. whoever wins, wins π
also warning: calling pthread_spin_unlock() on a lock that isn't held by the caller is undefined behavior too. don't do it, fr fr π
π₯ RETURN VALUE π₯
- on success, all three of these functions return zero π
- on failure, they return an error number π
π ERRORS π
pthread_spin_lock() π¬
she may fail with the following errors:
EDEADLOCKβ the system detected a deadlock condition. yeah. that vibe. π
pthread_spin_trylock() π¬
she fails with the following errors:
EBUSYβ the spin lock is currently locked by another thread. somebody else got there first, bestie, try again later π«
β STANDARDS β
POSIX.1-2008. certified, standardized, iconic π
π¦ HISTORY π¦
glibc 2.2β that's when she showed up in glibcPOSIX.1-2001β and that's the POSIX era she comes from
so she's been around a minute, lowkey a legend πΈ
π CAVEATS π
applying any of the functions described on this page to an uninitialized spin lock gives you undefined behavior. every single one. no exceptions. π¨
and like, seriously, go carefully read the NOTES section in pthread_spin_init(3). it's important, iykyk. don't skip it π
π SEE ALSO π
and that's the whole page, bestie!! π this was Linux man-pages 6.20, dated 2026-02-08 β go forth and spin responsibly (or better yet, don't spin, just call pthread_spin_trylock and live your truth) πβ¨π¦
π¦ 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
- Copyright 2017, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms