May 26, 2008 PTHREAD_SPIN_LOCK 3

### NAME

pthread_spin_lock, pthread_spin_trylock - acquire a spin lock

### LIBRARY

POSIX Thread Library (libpthread, -lpthread)

### SYNOPSIS

```int` pthread_spin_lock(```pthread_spinlock_t *lock`)
```int` pthread_spin_trylock(```pthread_spinlock_t *lock`)

### DESCRIPTION

The pthread_spin_lock(``) function acquires a spin lock on ```lock` provided that ```lock` is not presently held. If the lock cannot be immediately acquired, the calling thread repeatedly retries until it can acquire the lock.

The pthread_spin_trylock(``) function performs the same action, but does not block if the lock cannot be immediately obtained (i.e., the lock is held).

### RETURN VALUES

If successful, the pthread_spin_lock(``) and pthread_spin_trylock(``) functions will return zero. Otherwise an error number will be returned to indicate the error.

### ERRORS

The pthread_spin_trylock(``) function shall fail if:

[```EBUSY`]
  The lock could not be acquired because a writer holds the lock or was blocked on it.

The pthread_spin_lock(``) function may fail if:

[```EDEADLK`]
  The current thread already owns ```lock` for writing.

The pthread_spin_lock(``) and pthread_spin_trylock(``) functions may fail if:

[```EINVAL`]
  The value specified by ```lock` is invalid.

### SEE ALSO

pthread_spin_destroy(3), pthread_spin_init(3), pthread_spin_unlock(3)

### STANDARDS

pthread_spin_lock(``) and pthread_spin_trylock(``) conform to IEEE Std 1003.1-2001 (``POSIX.1'') .

### CAVEATS

Applications using spinlocks are vulnerable to the effects of priority inversion. Applications using real-time threads (```SCHED_FIFO`,`SCHED_RR`) should not use these interfaces. Outside carefully controlled environments, priority inversion with spinlocks can lead to system deadlock. Mutexes are preferable in nearly every possible use case.