POSIX Threads for Windows – REFERENCE - Pthreads-w32

Reference Index

Table of Contents


pthread_spin_destroy, pthread_spin_init - destroy or initialize a spin lock object (ADVANCED REALTIME THREADS)


#include <pthread.h>

pthread_spinlock_t lock = PTHREAD_SPINLOCK_INITIALIZER;

int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_init(pthread_spinlock_t *
lock, int pshared);


The pthread_spin_destroy function shall destroy the spin lock referenced by lock and release any resources used by the lock. The effect of subsequent use of the lock is undefined until the lock is reinitialized by another call to pthread_spin_init . The results are undefined if pthread_spin_destroy is called when a thread holds the lock, or if this function is called with an uninitialized thread spin lock.

The pthread_spin_init function shall allocate any resources required to use the spin lock referenced by lock and initialize the lock to an unlocked state.

Pthreads-w32 supports single and multiple processor systems as well as process CPU affinity masking by checking the mask when the spin lock is initialized. If the process is using only a single processor at the time pthread_spin_init is called then the spin lock is initialized as a PTHREAD_MUTEX_NORMAL mutex object. A thread that calls pthread_spin_lock(3) will block rather than spin in this case. If the process CPU affinity mask is altered after the spin lock has been initialised, the spin lock is not modified, and may no longer be optimal for the number of CPUs available.

Pthreads-w32 defines _POSIX_THREAD_PROCESS_SHARED in pthread.h as -1 to indicate that these routines do not support the PTHREAD_PROCESS_SHARED attribute. pthread_spin_init will return the error ENOTSUP if the value of pshared is not PTHREAD_PROCESS_PRIVATE.

The results are undefined if pthread_spin_init is called specifying an already initialized spin lock. The results are undefined if a spin lock is used without first being initialized.

If the pthread_spin_init function fails, the lock is not initialized and the contents of lock are undefined.

Only the object referenced by lock may be used for performing synchronization.

The result of referring to copies of that object in calls to pthread_spin_destroy , pthread_spin_lock(3) , pthread_spin_trylock(3), or pthread_spin_unlock(3) is undefined.

Pthreads-w32 supports statically initialized spin locks using PTHREAD_SPINLOCK_INITIALIZER. An application should still call pthread_spin_destroy at some point to ensure that any resources consumed by the spin lock are released.

Return Value

Upon successful completion, these functions shall return zero; otherwise, an error number shall be returned to indicate the error.


These functions may fail if:

The implementation has detected an attempt to initialize or destroy a spin lock while it is in use (for example, while being used in a pthread_spin_lock(3) call) by another thread.
The value specified by lock is invalid.

The pthread_spin_init function shall fail if:

The value of pshared is not PTHREAD_PROCESS_PRIVATE.
Insufficient memory exists to initialize the lock.

These functions shall not return an error code of [EINTR].

The following sections are informative.



Application Usage

The pthread_spin_destroy and pthread_spin_init functions are part of the Spin Locks option and need not be provided on all implementations.



Future Directions


See Also

pthread_spin_lock(3) , pthread_spin_unlock(3) , the Base Definitions volume of IEEE Std 1003.1-2001, <pthread.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Modified by Ross Johnson for use with Pthreads-w32.

Table of Contents