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

# pthread\_once.3

`pthread_once` — 动态包初始化

## 名称

`pthread_once`

## 库

libpthread

## 概要

```c
#include <pthread.h>

pthread_once_t once_control = PTHREAD_ONCE_INIT;

int
pthread_once(pthread_once_t *once_control,
    void (*init_routine)(void));
```

## 描述

进程中的任意线程首次以给定的 `once_control` 调用 `pthread_once` 时，将调用不带参数的 `init_routine`。后续以相同 `once_control` 对 `pthread_once` 的调用不会再调用 `init_routine`。从 `pthread_once` 返回时，可以保证 `init_routine` 已执行完毕。`once_control` 参数用于判断相关的初始化例程是否已被调用。

`pthread_once` 函数不是取消点。但如果 `init_routine` 是一个取消点且被取消，对 `once_control` 的影响等同于 `pthread_once` 从未被调用。

常量 `PTHREAD_ONCE_INIT` 由头文件

```c
#include <pthread.h>
```

定义。

如果 `once_control` 具有自动存储期或未由 `PTHREAD_ONCE_INIT` 初始化，`pthread_once` 的行为未定义。

## 返回值

如果成功，`pthread_once` 函数将返回零。否则返回一个错误编号以指示错误。

## 错误

无。

## 标准

`pthread_once` 函数遵循 ISO/IEC 9945-1:1996 (“POSIX.1”) 标准。


---

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