1 .\" Copyright (c) 2017, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH PTHREAD_SPIN_LOCK 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
7 pthread_spin_lock, pthread_spin_trylock, pthread_spin_unlock \-
8 lock and unlock a spin lock
11 .RI ( libpthread ", " \-lpthread )
14 .B #include <pthread.h>
16 .BI "int pthread_spin_lock(pthread_spinlock_t *" lock );
17 .BI "int pthread_spin_trylock(pthread_spinlock_t *" lock );
18 .BI "int pthread_spin_unlock(pthread_spinlock_t *" lock );
22 Feature Test Macro Requirements for glibc (see
23 .BR feature_test_macros (7)):
26 .BR pthread_spin_lock (),
27 .BR pthread_spin_trylock ():
29 _POSIX_C_SOURCE >= 200112L
33 .BR pthread_spin_lock ()
34 function locks the spin lock referred to by
36 If the spin lock is currently unlocked,
37 the calling thread acquires the lock immediately.
38 If the spin lock is currently locked by another thread,
39 the calling thread spins, testing the lock until it becomes available,
40 at which point the calling thread acquires the lock.
43 .BR pthread_spin_lock ()
44 on a lock that is already held by the caller
45 or a lock that has not been initialized with
46 .BR pthread_spin_init (3)
47 results in undefined behavior.
50 .BR pthread_spin_trylock ()
52 .BR pthread_spin_lock (),
53 except that if the spin lock referred to by
56 then, instead of spinning, the call returns immediately with the error
60 .BR pthread_spin_unlock ()
61 function unlocks the spin lock referred to
63 If any threads are spinning on the lock,
64 one of those threads will then acquire the lock.
67 .BR pthread_spin_unlock ()
68 on a lock that is not held by the caller results in undefined behavior.
70 On success, these functions return zero.
71 On failure, they return an error number.
73 .BR pthread_spin_lock ()
74 may fail with the following errors:
77 .\" Not detected in glibc
78 The system detected a deadlock condition.
80 .BR pthread_spin_trylock ()
81 fails with the following errors:
84 The spin lock is currently locked by another thread.
86 These functions first appeared in glibc in version 2.2.
90 Applying any of the functions described on this page to
91 an uninitialized spin lock results in undefined behavior.
93 Carefully read NOTES in
94 .BR pthread_spin_init (3).
98 .\" FIXME . .BR pthread_mutex_lock (3),
99 .BR pthread_spin_destroy (3),
100 .BR pthread_spin_init (3),