Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


pthread_spin_init(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |HISTORY |NOTES |SEE ALSO |COLOPHON

pthread_spin_init(3)     Library Functions Manualpthread_spin_init(3)

NAME        top

       pthread_spin_init, pthread_spin_destroy - initialize or destroy a       spin lock

LIBRARY        top

       POSIX threads library (libpthread,-lpthread)

SYNOPSIS        top

#include <pthread.h>int pthread_spin_init(pthread_spinlock_t *lock, intpshared);int pthread_spin_destroy(pthread_spinlock_t *lock);   Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):pthread_spin_init(),pthread_spin_destroy():           _POSIX_C_SOURCE >= 200112L

DESCRIPTION        top

General note: Most programs should use mutexes instead of spin       locks.  Spin locks are primarily useful in conjunction with real-       time scheduling policies.  See NOTES.       Thepthread_spin_init() function allocates any resources required       for the use of the spin lock referred to bylock and initializes       the lock to be in the unlocked state.  Thepshared argument must       have one of the following values:PTHREAD_PROCESS_PRIVATE              The spin lock is to be operated on only by threads in the              same process as the thread that callspthread_spin_init().              (Attempting to share the spin lock between processes              results in undefined behavior.)PTHREAD_PROCESS_SHARED              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).       Callingpthread_spin_init() on a spin lock that has already been       initialized results in undefined behavior.       Thepthread_spin_destroy() 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.       Once a spin lock has been destroyed, performing any operation on       the lock other than once more initializing it withpthread_spin_init() results in undefined behavior.       The result of performing operations such aspthread_spin_lock(3),pthread_spin_unlock(3), andpthread_spin_destroy() oncopies of       the object referred to bylock is undefined.

RETURN VALUE        top

       On success, there functions return zero.  On failure, they return       an error number.  In the event thatpthread_spin_init() fails, the       lock is not initialized.

ERRORS        top

pthread_spin_init() may fail with the following errors:EAGAINThe system has insufficient resources to initialize a new              spin lock.ENOMEMInsufficient memory to initialize the spin lock.

STANDARDS        top

       POSIX.1-2008.

HISTORY        top

       glibc 2.2.  POSIX.1-2001.       Support for process-shared spin locks is a POSIX option.  The       option is supported in the glibc implementation.

NOTES        top

       Spin locks should be employed in conjunction with real-time       scheduling policies (SCHED_FIFO, or possiblySCHED_RR).  Use of       spin locks with nondeterministic scheduling policies such asSCHED_OTHERprobably 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.       If threads create a deadlock situation while employing spin locks,       those threads will spin forever consuming CPU time.       User-space spin locks arenot 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.

SEE ALSO        top

pthread_mutex_init(3),pthread_mutex_lock(3),pthread_spin_lock(3),pthread_spin_unlock(3),pthreads(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17pthread_spin_init(3)

Pages that refer to this page:pthread_spin_lock(3)pthreads(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp