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

# pthread\_detach.3

`pthread_detach` — 分离线程

## 名称

`pthread_detach`

## 库

libpthread

## 概要

```c
#include <pthread.h>

int
pthread_detach(pthread_t thread);
```

## 描述

`pthread_detach` 函数用于向实现指示：当线程 `thread` 终止时，可以回收该线程的存储资源。如果 `thread` 尚未终止，`pthread_detach` 不会导致其终止。对同一目标线程多次调用 `pthread_detach` 的效果未定义。

## 返回值

如果成功，`pthread_detach` 函数将返回零；否则将返回一个错误号以指示错误。注意，该函数不会像某些标准草案中那样改变 `errno` 的值。这些早期草案还将指向 pthread\_t 的指针作为参数传递。务必注意！

## 错误

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

**`[EINVAL]`** 实现检测到 `thread` 指定的值不引用一个可汇合的线程。

**`[ESRCH]`** 找不到与给定的线程 ID `thread` 相对应的线程。

## 参见

[pthread\_join(3)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man3/pthread_join.3.md)

## 标准

`pthread_detach` 函数符合 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_detach.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.
