从 pthreads 的手册页:
Over time, two threading implementations have been provided by the
GNU C library on Linux:
LinuxThreads
This is the original Pthreads implementation. Since glibc
2.4, this implementation is no longer supported.
NPTL (Native POSIX Threads Library)
This is the modern Pthreads implementation. By comparison
with LinuxThreads, NPTL provides closer conformance to the
requirements of the POSIX.1 specification and better
performance when creating large numbers of threads. NPTL is
available since glibc 2.3.2, and requires features that are
present in the Linux 2.6 kernel.
Both of these are so-called 1:1 implementations, meaning that each
thread maps to a kernel scheduling entity. Both threading
implementations employ the Linux clone(2) system call. In NPTL,
thread synchronization primitives (mutexes, thread joining, and so
on) are implemented using the Linux futex(2) system call.
来自 man futex(7):
In its bare form, a futex is an aligned integer which is touched only
by atomic assembler instructions. Processes can share this integer
using mmap(2), via shared memory segments or because they share
memory space, in which case the application is commonly called
multithreaded.
在此处找到的附加说明:
(如果你想知道它们是如何在共享内存中工作的:Futex 被锁定在它们的物理地址上)
总而言之,Linux 决定在其“本机”原语之上实现 pthread futex
,该原语确实存在于用户进程地址空间中。对于共享同步原语,这将是共享内存,并且在一个进程死亡后,其他进程仍然可以看到它。
如果进程终止会发生什么?Ingo Molnar 就此写了一篇名为Robust Futexes的文章。相关报价:
强大的 Futex
但是可能存在一场比赛:由于在 glibc 获取 futex 之后完成添加和删除列表,因此有一些指令窗口供线程(或进程)死在那里,使 futex 挂起。为了防止这种可能性,用户空间 (glibc) 还维护了一个简单的每个线程的“list_op_pending”字段,以允许内核在线程在获取锁后死亡时进行清理,但就在它可以将自己添加到列表之前。Glibc 在尝试获取 futex 之前设置这个 list_op_pending 字段,并在 list-add(或 list-remove)完成后清除它
概括
这让你离开其他平台,是开放式的。可以这么说,至少 Linux 实现已经非常小心地满足了我们对健壮性的常识性期望。
看到其他操作系统通常首先求助于基于内核的同步原语,我有理由假设它们的实现会更加自然地健壮。