man-pages/man3/pthread_spin_init.3.html
2021-03-31 01:06:50 +01:00

255 lines
6.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PTHREAD_SPIN_INIT</TITLE>
</HEAD><BODY>
<H1>PTHREAD_SPIN_INIT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-30<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
pthread_spin_init, pthread_spin_destroy - initialize or destroy a spin lock
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</B>
<B>int pthread_spin_init(pthread_spinlock_t *</B><I>lock</I><B>, int </B><I>pshared</I><B>);</B>
<B>int pthread_spin_destroy(pthread_spinlock_t *</B><I>lock</I><B>);</B>
</PRE>
<P>
Compile and link with <I>-pthread</I>.
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>pthread_spin_init</B>(),
<B>pthread_spin_destroy</B>():
<BR>
<DL COMPACT><DT id="1"><DD>
_POSIX_C_SOURCE &gt;= 200112L
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<I>General note</I>:
Most programs should use mutexes
instead of spin locks.
Spin locks are primarily useful in conjunction with real-time
scheduling policies.
See NOTES.
<P>
The
<B>pthread_spin_init</B>()
function allocates any resources required for the use of
the spin lock referred to by
<I>lock</I>
and initializes the lock to be in the unlocked state.
The
<I>pshared</I>
argument must have one of the following values:
<DL COMPACT>
<DT id="2"><B>PTHREAD_PROCESS_PRIVATE</B>
<DD>
The spin lock is to be operated on only by threads in the same process
as the thread that calls
<B>pthread_spin_init</B>().
(Attempting to share the spin lock between processes
results in undefined behavior.)
<DT id="3"><B>PTHREAD_PROCESS_SHARED</B>
<DD>
The spin lock may be operated on by any thread in any process that
has access to the memory containing the lock
(i.e., the lock may be in a shared memory object that is
shared among multiple processes).
</DL>
<P>
Calling
<B>pthread_spin_init</B>()
on a spin lock that has already been initialized results
in undefined behavior.
<P>
The
<B>pthread_spin_destroy</B>()
function destroys a previously initialized spin lock,
freeing any resources that were allocated for that lock.
Destroying a spin lock that has not been previously been initialized
or destroying a spin lock while another thread holds the lock
results in undefined behavior.
<P>
Once a spin lock has been destroyed,
performing any operation on the lock other than
once more initializing it with
<B>pthread_spin_init</B>()
results in undefined behavior.
<P>
The result of performing operations such as
<B><A HREF="/cgi-bin/man/man2html?3+pthread_spin_lock">pthread_spin_lock</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_spin_unlock">pthread_spin_unlock</A></B>(3),
and
<B><A HREF="/cgi-bin/man/man2html?3+pthread_spin_destroy">pthread_spin_destroy</A></B>(3)
on
<I>copies</I>
of the object referred to by
<I>lock</I>
is undefined.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, there functions return zero.
On failure, they return an error number.
In the event that
<B>pthread_spin_init</B>()
fails, the lock is not initialized.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<B>pthread_spin_init</B>()
may fail with the following errors:
<DL COMPACT>
<DT id="4"><B>EAGAIN</B>
<DD>
The system has insufficient resources to initialize
a new spin lock.
<DT id="5"><B>ENOMEM</B>
<DD>
Insufficient memory to initialize the spin lock.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
These functions first appeared in glibc in version 2.2.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001.
<P>
Support for process-shared spin locks is a POSIX option.
The option is supported in the glibc implementation.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Spin locks should be employed in conjunction with
real-time scheduling policies
(<B>SCHED_FIFO</B>,
or possibly
<B>SCHED_RR</B>).
Use of spin locks with nondeterministic scheduling policies such as
<B>SCHED_OTHER</B>
probably indicates a design mistake.
The problem is that if a thread operating under such a policy
is scheduled off the CPU while it holds a spin lock,
then other threads will waste time spinning on the lock
until the lock holder is once more rescheduled and releases the lock.
<P>
If threads create a deadlock situation while employing spin locks,
those threads will spin forever consuming CPU time.
<P>
User-space spin locks are
<I>not</I>
applicable as a general locking solution.
They are, by definition,
prone to priority inversion and unbounded spin times.
A programmer using spin locks must be exceptionally careful not
only in the code, but also in terms of system configuration,
thread placement, and priority assignment.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_init">pthread_mutex_init</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_mutex_lock">pthread_mutex_lock</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_spin_lock">pthread_spin_lock</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_spin_unlock">pthread_spin_unlock</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="12"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAI">NOTES</A><DD>
<DT id="14"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:53 GMT, March 31, 2021
</BODY>
</HTML>