> 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_init.3.md).

# pthread\_spin\_init.3

`pthread_spin_init` — 初始化或销毁自旋锁

## 名称

`pthread_spin_init`

## 库

libpthread

## 概要

```c
#include <pthread.h>

int
pthread_spin_init(pthread_spinlock_t *lock, int pshared)

int
pthread_spin_destroy(pthread_spinlock_t *lock)
```

## 描述

`pthread_spin_init` 函数将 `lock` 初始化为未锁定状态，并分配开始使用它所需的任何资源。如果 `pshared` 设置为 `PTHREAD_PROCESS_SHARED`，任何线程，无论是否属于创建自旋锁的进程，只要能访问 `lock` 所在的内存区域，都可以使用 `lock`。如果设置为 `PTHREAD_PROCESS_PRIVATE`，则只能被同一进程内的线程使用。

`pthread_spin_destroy` 函数将销毁 `lock` 并释放为其分配的任何资源。

## 返回值

如果成功，`pthread_spin_init` 和 `pthread_spin_destroy` 都将返回零。否则将返回一个错误号以指示错误。

这两个函数都不会返回 `EINTR`。

## 错误

`pthread_spin_init` 和 `pthread_spin_destroy` 函数将在以下情况失败：

**`[EBUSY]`** 尝试在 `lock` 正在使用时初始化或销毁它。

**`[EINVAL]`** 由 `lock` 指定的值无效。

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

**`[EAGAIN]`** 除内存外资源不足，无法初始化 `lock`。

**`[ENOMEM]`** 内存不足，无法初始化 `lock`。

## 参见

[pthread\_spin\_lock(3)](/man/pthread/pthread_spin_lock.3.md), pthread\_spin\_unlock(3)

## 历史

`pthread_spin_init` 和 `pthread_spin_destroy` 函数首次出现于 FreeBSD 5.2 的 libkse 中，以及 FreeBSD 5.3 的 libthr 中。对进程共享自旋锁的支持出现于 FreeBSD 11.0。


---

# 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_init.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.
