Multithreaded Programming Guide

Acquiring a Non-Blocking Spin Lock

Use the pthread_spin_trylock(3C) function to lock a spin lock and fail immediately if the lock is held by another thread.

pthread_spin_trylock() Syntax

int  pthread_spin_trylock(pthread_spinlock_t *lock);
#include <pthread.h>

pthread_spinlock_t lock;
int ret;

ret = pthread_spin_trylock(&lock); /* try to lock the spin lock */

pthread_spin_trylock() Return Values

Upon successful completion, the pthread_spin_trylock() function returns 0. Otherwise, one of the following error codes is returned.



A thread currently owns the spin lock.



The value specified by lock does not refer to an initialized spin lock object.