1PTHREAD_SPIN_INIT(3) Linux Programmer's Manual PTHREAD_SPIN_INIT(3)
2
3
4
6 pthread_spin_init, pthread_spin_destroy - initialize or destroy a spin
7 lock
8
10 #include <pthread.h>
11
12 int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
13 int pthread_spin_destroy(pthread_spinlock_t *lock);
14
15 Compile and link with -pthread.
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 pthread_spin_init(), pthread_spin_destroy():
20 _POSIX_C_SOURCE >= 200112L
21
23 General note: Most programs should use mutexes instead of spin locks.
24 Spin locks are primarily useful in conjunction with real-time schedul‐
25 ing policies. See NOTES.
26
27 The pthread_spin_init() function allocates any resources required for
28 the use of the spin lock referred to by lock and initializes the lock
29 to be in the unlocked state. The pshared argument must have one of the
30 following values:
31
32 PTHREAD_PROCESS_PRIVATE
33 The spin lock is to be operated on only by threads in the same
34 process as the thread that calls pthread_spin_init(). (Attempt‐
35 ing to share the spin lock between processes results in unde‐
36 fined behavior.)
37
38 PTHREAD_PROCESS_SHARED
39 The spin lock may be operated on by any thread in any process
40 that has access to the memory containing the lock (i.e., the
41 lock may be in a shared memory object that is shared among mul‐
42 tiple processes).
43
44 Calling pthread_spin_init() on a spin lock that has already been ini‐
45 tialized results in undefined behavior.
46
47 The pthread_spin_destroy() function destroys a previously initialized
48 spin lock, freeing any resources that were allocated for that lock.
49 Destroying a spin lock that has not been previously been initialized or
50 destroying a spin lock while another thread holds the lock results in
51 undefined behavior.
52
53 Once a spin lock has been destroyed, performing any operation on the
54 lock other than once more initializing it with pthread_spin_init() re‐
55 sults in undefined behavior.
56
57 The result of performing operations such as pthread_spin_lock(3),
58 pthread_spin_unlock(3), and pthread_spin_destroy() on copies of the ob‐
59 ject referred to by lock is undefined.
60
62 On success, there functions return zero. On failure, they return an
63 error number. In the event that pthread_spin_init() fails, the lock is
64 not initialized.
65
67 pthread_spin_init() may fail with the following errors:
68
69 EAGAIN The system has insufficient resources to initialize a new spin
70 lock.
71
72 ENOMEM Insufficient memory to initialize the spin lock.
73
75 These functions first appeared in glibc in version 2.2.
76
78 POSIX.1-2001.
79
80 Support for process-shared spin locks is a POSIX option. The option is
81 supported in the glibc implementation.
82
84 Spin locks should be employed in conjunction with real-time scheduling
85 policies (SCHED_FIFO, or possibly SCHED_RR). Use of spin locks with
86 nondeterministic scheduling policies such as SCHED_OTHER probably indi‐
87 cates a design mistake. The problem is that if a thread operating un‐
88 der such a policy is scheduled off the CPU while it holds a spin lock,
89 then other threads will waste time spinning on the lock until the lock
90 holder is once more rescheduled and releases the lock.
91
92 If threads create a deadlock situation while employing spin locks,
93 those threads will spin forever consuming CPU time.
94
95 User-space spin locks are not applicable as a general locking solution.
96 They are, by definition, prone to priority inversion and unbounded spin
97 times. A programmer using spin locks must be exceptionally careful not
98 only in the code, but also in terms of system configuration, thread
99 placement, and priority assignment.
100
102 pthread_mutex_init(3), pthread_mutex_lock(3), pthread_spin_lock(3),
103 pthread_spin_unlock(3), pthreads(7)
104
106 This page is part of release 5.12 of the Linux man-pages project. A
107 description of the project, information about reporting bugs, and the
108 latest version of this page, can be found at
109 https://www.kernel.org/doc/man-pages/.
110
111
112
113Linux 2021-03-22 PTHREAD_SPIN_INIT(3)