> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/pthread/pthread_spin_lock.3.md).

# pthread\_spin\_lock.3

`pthread_spin_lock` — 锁定或解锁自旋锁

## 名称

`pthread_spin_lock`

## 库

libpthread

## 概要

```c
#include <pthread.h>

int
pthread_spin_lock(pthread_spinlock_t *lock)

int
pthread_spin_trylock(pthread_spinlock_t *lock)

int
pthread_spin_unlock(pthread_spinlock_t *lock)
```

## 描述

`pthread_spin_lock` 函数将在 `lock` 当前未被其他线程持有时获取它。如果无法立即获取锁，它将自旋尝试获取锁（不会睡眠）直到锁可用。

`pthread_spin_trylock` 函数与 `pthread_spin_lock` 相同，区别在于如果无法立即获取 `lock`，将返回错误。

`pthread_spin_unlock` 函数将释放 `lock`，该锁必须先前已通过调用 `pthread_spin_lock` 或 `pthread_spin_trylock` 锁定。

## 返回值

如果成功，所有这些函数都将返回零。否则将返回一个错误号以指示错误。

这些函数都不会返回 `EINTR`。

## 错误

`pthread_spin_lock`、`pthread_spin_trylock` 和 `pthread_spin_unlock` 函数将在以下情况失败：

**`[EINVAL]`** 由 `lock` 指定的值无效或未初始化。

`pthread_spin_lock` 函数可能在以下情况失败：

**`[EDEADLK]`** 调用线程已持有该锁。

`pthread_spin_trylock` 函数将在以下情况失败：

**`[EBUSY]`** 另一个线程当前持有 `lock`。

`pthread_spin_unlock` 函数可能在以下情况失败：

**`[EPERM]`** 调用线程未持有 `lock`。

## 参见

pthread\_spin\_destroy(3), [pthread\_spin\_init(3)](/man/pthread/pthread_spin_init.3.md)

## 历史

`pthread_spin_lock`、`pthread_spin_trylock` 和 `pthread_spin_unlock` 函数首次出现于 FreeBSD 5.2 的 libkse 中，以及 FreeBSD 5.3 的 libthr 中。

## 缺陷

`pthread_spin_lock`、`pthread_spin_trylock` 和 `pthread_spin_unlock` 的实现预期符合 IEEE Std 1003.2 ("POSIX.2") 规范。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/pthread/pthread_spin_lock.3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
