# POSIX 线程

**POSIX 线程**（POSIX threads），通常称为 **Pthreads**，是 POSIX.1 为 C 语言线程编程规定的一组接口（函数和头文件）。单个进程可以包含多个线程，它们都执行同一程序：线程共享同一全局内存（数据段和堆段），而每个线程都有自己的栈用于自动变量。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> 这些线程接口最初作为 IEEE Std 1003.1c-1995（即 POSIX 线程扩展）标准化，现在是 IEEE/Open Group 联合标准 POSIX.1-2024（The Open Group 基础规范第 8 版）的一部分。<sup>[2](https://standards.ieee.org/ieee/1003.1c/1393/)</sup><sup> • </sup><sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 标准起源 | IEEE Std 1003.1c-1995，线程扩展（C 语言）；标准委员会于 1995 年 6 月 14 日批准，1996 年 7 月 12 日发布，现为已被取代的标准<sup>[2](https://standards.ieee.org/ieee/1003.1c/1393/)</sup> |
| 共享状态与每线程状态 | 线程共享进程 ID、打开的文件描述符、信号处置和资源限制；每个线程有自己的线程 ID、信号掩码、errno 值、备用信号栈以及实时调度策略和优先级<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> |
| 返回约定 | 大多数 pthreads 函数成功时返回 0，失败时返回错误编号；它们不设置 errno，POSIX.1-2001 规定它们绝不会因 EINTR 失败<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> |
| 线程 ID | 仅在一个进程内唯一；系统可以在已终止线程被汇合之后，或分离线程终止之后，重用线程 ID<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> |
| 线程安全 | POSIX.1-2001 和 POSIX.1-2008 要求所有标准函数都是线程安全的，但明确列出的若干函数除外，其中包括 asctime()、strtok()、rand()、getenv() 和 localtime()<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> |
| Linux 实现 | LinuxThreads（原始实现，自 glibc 2.4 起不再支持）和 NPTL（自 glibc 2.3.2 起可用，需要 Linux 2.6 特性）；两者都是使用 clone(2) 的 1:1 实现，NPTL 在 futex(2) 上构建互斥量和汇合操作<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> |
| 当前版本 | POSIX.1-2024（第 8 版），由 IEEE 和 The Open Group 共同制定<sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup> |

## API 涵盖的内容

Pthreads API 围绕三大类例程组织：线程管理（在共享地址空间下，一个进程中多个控制流的创建、控制和终止）、互斥量变量和条件变量。<sup>[4](https://cs.unibo.it/~ghini/didattica/sistop/pthreads_tutorial/POSIX_Threads_Programming.htm)</sup> 最初的 1003.1c 标准包括线程控制、线程属性、优先级调度、互斥量、互斥量优先级继承、互斥量优先级上限和条件变量。<sup>[5](https://www.mitre.org/sites/default/files/pdf/obenland_posix.pdf)</sup>

**线程创建与 ID。** 线程通过 pthread_create() 创建，该函数返回 pthread_t 类型的标识符；线程通过 pthread_self() 获得自己的标识符。线程 ID 只保证在一个进程内唯一，POSIX 指出，使用生命周期已经结束的线程 ID 会产生未定义行为。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> POSIX.1-2024 将唯一性扩展到由 pthread_create()、thrd_create()（ISO C 线程接口）和 SIGEV_THREAD 通知创建的线程之间，这要求 pthread_t 和 thrd_t 是相同的底层类型。<sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup>

**互斥量与条件变量。** 互斥量是一种低层原语，其他同步函数可以构建在其上；线程通过成功的加锁调用成为其所有者，并一直保持所有者身份，直到它解锁或在条件等待中阻塞。<sup>[6](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html)</sup> 条件变量总是与互斥量配合使用：pthread_cond_wait() 临时释放互斥量，等待信号，并在返回时重新获取互斥量。<sup>[7](https://my.eng.utah.edu/~cs5966/LECTURES/Week1/pthread-exs/posix-programming.htm)</sup> 标准的理由说明指出，当条件等待无错误返回时，相关谓词可能仍为假，因此应用程序必须在任何返回时重新检查谓词。<sup>[8](https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cond_clockwait.html)</sup>

**汇合与终止。** 线程通过执行 pthread_exit()、从其启动例程返回，或响应来自 pthread_cancel() 的取消请求而终止。<sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup> pthread_join() 获取线程的 void * 退出状态；它最初在标准的第 5 版中发布，并在第 7 版中从线程选项移入基础部分，因此现在每个符合标准的系统都提供它。<sup>[9](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/pthread_join.html)</sup>

## 取消与线程安全

取消是协作式的。三种可取消性状态（禁用、延迟和异步）编码在两个独立的位中，因此可以独立地更改和恢复。取消点是某些函数内部的特定位置，当可取消性启用时，线程必须在这些位置对挂起的取消请求采取行动；条件等待是取消点，因为所等待的事件可能永远不会发生。<sup>[6](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html)</sup> 当取消请求被处理，或线程调用 pthread_exit() 时，通过 pthread_cleanup_push() 压入的清理处理程序按后进先出（LIFO）顺序逐个运行。<sup>[6](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html)</sup>

线程安全函数是指可以同时从多个线程安全调用的函数。POSIX.1-2001 和 POSIX.1-2008 要求标准中规定的每个函数都是线程安全的，但明确列出的若干使用静态存储的函数除外，例如 asctime()、ctime()、getenv()、gmtime()、localtime()、rand() 和 strtok()。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> 标准还规定了哪些函数会同步内存：互斥量上的加锁和解锁操作、条件等待、pthread_create()、pthread_join()，以及自旋锁和读写锁函数，都会在成功调用时同步内存，这正是能够避免普通数据竞争的原因。<sup>[10](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)</sup>

## 标准化历史

线程工作最初是 POSIX 项目 1003.4a，后来成为 IEEE Std 1003.1c-1995，于 1995 年 6 月 14 日由 IEEE 标准委员会批准，1996 年 6 月 7 日获 ANSI 批准，1996 年 7 月 12 日发布；它现在是一项已被取代的标准。<sup>[2](https://standards.ieee.org/ieee/1003.1c/1393/)</sup> POSIX.1 的 1996 年版（ISO/IEC 9945-1:1996）将线程扩展与实时扩展 1003.1b-1993 和 1003.1i-1995 一并纳入。<sup>[11](https://www.usenix.org/legacy/publications/login/standards/34.posix.html)</sup>

当 POSIX.1c 被纳入[单一 UNIX 规范](https://www.edgepedia.org/zh/single-unix-specification)第 2 版时，其大部分可选功能被改为强制功能。在其之上添加的 X/Open 线程扩展来自 Aspen Group，这是一个由 UNIX 厂商于 1995 年底组成的小组，目的是将数据库引擎和其他应用程序已经依赖的常见专有扩展标准化。它新增的内容包括扩展互斥量属性类型（尤其是互斥量类型属性，该属性定义了 POSIX.1c 未作规定的行为）、读写锁及其属性、线程并发级别、栈保护大小和并行 I/O。<sup>[12](https://usenix.org/legacy/publications/login/standards/17.single.html)</sup> 读写锁允许多个线程同时进行只读访问，而写者持有独占访问权，建议用于经常被引用但很少修改的数据。<sup>[12](https://usenix.org/legacy/publications/login/standards/17.single.html)</sup>

当前版本 POSIX.1-2024 在技术上与 The Open Group 基础规范第 8 版相同，并使线程接口与 ISO/IEC 9899:2018（C17）保持一致，包括 <threads.h> 中的函数，如 thrd_create() 和 thrd_join()。<sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup><sup> • </sup><sup>[13](https://pubs.opengroup.org/onlinepubs/9799919799/functions/thrd_join.html)</sup> 在调度理由说明中，曾考虑速率单调调度，但在 pthreads 的语境下未将其纳入标准化，而零星服务器策略则被纳入。<sup>[14](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xsh_chap01.html)</sup>

## Linux 实现

GNU C 库在 Linux 上提供了两种线程实现。LinuxThreads 是最初的 Pthreads 实现，自 glibc 2.4 起不再受支持。NPTL（原生 POSIX 线程库）是现代实现，自 glibc 2.3.2 起可用，并且需要 Linux 2.6 内核中存在的特性；与 LinuxThreads 相比，它更符合 POSIX.1，并且在创建大量线程时性能更好。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup>

两者都是 1:1 实现，即每个线程映射到一个内核调度实体，并且都使用 Linux clone(2) 系统调用。在 NPTL 中，互斥量和线程汇合等同步原语是用 futex(2) 系统调用实现的。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup>

**LinuxThreads 的偏离。** LinuxThreads 创建了一个额外的“管理者”线程来处理线程创建和终止，其线程不共享进程 ID，在 ps(1) 中显示为独立进程。它在许多方面偏离了 POSIX.1：getpid() 在每个线程中返回不同的值，线程不共享用户 ID 和组 ID、会话 ID 和进程组 ID、记录锁、信号量撤销值、间隔定时器或共同的 nice 值，而且它完全不支持面向进程的信号。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup>

**NPTL 的行为。** 使用 NPTL 时，一个进程中的所有线程都放在同一个线程组中并共享同一个 PID，而且没有管理者线程。NPTL 在内部使用前两个实时信号，因此应用程序不能使用它们。它与 POSIX.1 剩余的不符合之处是线程不共享共同的 nice 值；其他若干不符合之处只出现在较旧的内核上，并已在 Linux 2.6.9 到 2.6.16 中修复，包括 times(2) 和 getrusage(2) 信息按线程而非按进程提供（在 2.6.9 中修复）以及资源限制不共享（在 2.6.10 中修复）。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup> 自 glibc 2.3.2 起，getconf GNU_LIBPTHREAD_VERSION 会报告系统使用哪种实现。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup>

在 Linux 上使用 Pthreads API 的程序应使用 cc -pthread 编译。<sup>[1](https://man7.org/linux/man-pages/man7/pthreads.7.html)</sup>

## Pthreads 与其他线程接口

Pthreads 是一个相对低层的 C 语言线程库，提供两种基本并发机制：线程和锁，并已成为 C 语言并发编程的事实标准，大量应用程序和库构建在其上。<sup>[15](https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC%2B%2B.pdf)</sup> 更高层的替代方案以控制换取便利：OpenMP 层次更高，跨编译器可移植性更好，并且不限于 C，但用它达到相同性能时，可能比 pthreads 需要更少的直接调优。<sup>[16](https://stackoverflow.com/questions/3949901/pthreads-vs-openmp)</sup> POSIX 接口仍然是这些封装层所调用的层，POSIX.1-2024 通过要求 pthread_* 与 thrd_* 系列之间共享线程 ID 语义，使二者保持一致。<sup>[3](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)</sup>

## References

1. [pthreads(7) - Linux manual page, Linux man-pages project](https://man7.org/linux/man-pages/man7/pthreads.7.html)
2. [IEEE SA - IEEE 1003.1c-1995, Threads Extension (C Language)](https://standards.ieee.org/ieee/1003.1c/1393/)
3. [POSIX.1-2024, Base Definitions, Definitions chapter (The Open Group / IEEE)](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html)
4. [POSIX Threads Programming tutorial, University of Bologna mirror](https://cs.unibo.it/~ghini/didattica/sistop/pthreads_tutorial/POSIX_Threads_Programming.htm)
5. [The Use of POSIX in Real-time Systems, Assessing its Effectiveness and Performance, MITRE](https://www.mitre.org/sites/default/files/pdf/obenland_posix.pdf)
6. [POSIX.1-2024, System Interfaces, General Information chapter 2](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html)
7. [POSIX Threads Programming, University of Utah course notes](https://my.eng.utah.edu/~cs5966/LECTURES/Week1/pthread-exs/posix-programming.htm)
8. [pthread_cond_clockwait, POSIX.1-2024 System Interfaces](https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cond_clockwait.html)
9. [pthread_join, POSIX.1-2024 System Interfaces](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/pthread_join.html)
10. [POSIX.1-2024, Base Definitions, General Concepts chapter 4, Memory Synchronization](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)
11. [The POSIX Bookshelf, USENIX ;login: Standards Reports](https://www.usenix.org/legacy/publications/login/standards/34.posix.html)
12. [The Single UNIX Specification, Version 2: Threads Extensions, USENIX ;login: (Andrew Josey)](https://usenix.org/legacy/publications/login/standards/17.single.html)
13. [thrd_join, POSIX.1-2024 System Interfaces](https://pubs.opengroup.org/onlinepubs/9799919799/functions/thrd_join.html)
14. [POSIX.1-2024, Rationale for System Interfaces](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xsh_chap01.html)
15. [Posix threads, uC++ documentation, University of Waterloo](https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC%2B%2B.pdf)
16. [Pthreads vs. OpenMP, Stack Overflow](https://stackoverflow.com/questions/3949901/pthreads-vs-openmp)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/posix-threads

English original: https://www.edgepedia.org/posix-threads
