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

# pthread\_cancel.3

`pthread_cancel` — 取消线程的执行

## 名称

`pthread_cancel`

## 库

Lb libpthread

## 概要

`#include <pthread.h>`

`Ft int Fn pthread_cancel pthread_t thread`

## 描述

`Fn pthread_cancel` 函数请求取消 `thread`。目标线程的可取消状态和类型决定了取消何时生效。当取消操作被执行时，将调用 `thread` 的取消清理处理程序。当最后一个取消清理处理程序返回时，将为 `thread` 调用线程特定数据析构函数。当最后一个析构函数返回时，`thread` 将被终止。

目标线程中的取消处理相对于调用线程从 `Fn pthread_cancel` 返回是异步进行的。

状态 `PTHREAD_CANCELED` 会对与目标线程汇合的任何线程可见。符号常量 `PTHREAD_CANCELED` 展开为类型为 `Ft (void *)` 的常量表达式，其值不与内存中任何对象的指针匹配，也不与值 `NULL` 匹配。

## 返回值

若成功，`Fn pthread_cancel` 函数将返回零。否则将返回一个错误号以指示错误。

## 错误

`Fn pthread_cancel` 函数在以下情况下会失败：

**\[Er** ESRCH] 找不到与给定线程 ID 对应的线程。

## 参见

[pthread\_cleanup\_pop(3)](/man/pthread/pthread_cleanup_pop.3.md), [pthread\_cleanup\_push(3)](/man/pthread/pthread_cleanup_push.3.md), [pthread\_exit(3)](/man/pthread/pthread_exit.3.md), [pthread\_join(3)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man3/pthread_join.3.md), pthread\_setcancelstate(3), pthread\_setcanceltype(3), [pthread\_testcancel(3)](/man/pthread/pthread_testcancel.3.md)

## 标准

`Fn pthread_cancel` 函数遵循 ISO/IEC 9945-1:1996 ("POSIX.1") 标准。

## 作者

本手册页由 David Leonard <d@openbsd.org> 为 OpenBSD 的 `Fn pthread_cancel` 实现编写。


---

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