POSIX 线程在 Linux 上是如何实现的?
我想知道通过 pthreads 库创建的线程实际上是内核级线程还是与内核无关的用户空间线程?我听到过对此相互排斥的意见,所以我想知道真相。
I was wondering if threads created via the pthreads library are actually kernel-level threads or user-space threads that have nothing to do with the kernel? I have heard mutually exclusive opinions about this, so I want to know the truth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Linux 2.6 之前,它们本质上是用户空间线程,是粘合在一起的独立进程,因为内核没有真正的线程支持。编辑:2.6 之前对内核级线程(clone() 函数)有一些有限的支持,但它不与 posix 线程一起使用,仅与名为 linuxthreads 的替代线程库一起使用。
自从 NPTL(本机 Posix 线程库)出现以来,它们都是内核线程。
Before Linux 2.6 they were essentially userspace threads, separate processes which were glued together, because the kernel had no real thread support. Edit: There was some limited support for kernel level threads (a clone() function) before 2.6, but it wasn't used with posix threads, only with an alternative thread library called linuxthreads.
Since the arrival of NPTL (Native Posix Thread Library) the are kernel threads.
Linux 上
pthread_create()
创建的线程始终是内核级线程。 LinuxThreads 并不完全符合 POSIX(同一进程中的线程具有不同的 pid,信号处理与 POSIX 要求的不同,...),因此创建 NPTL 来解决这些问题。有些库实现了用户级线程(例如:GNU pth,p 代表“可移植”),但它们不使用 POSIX 线程 API。
Threads created by
pthread_create()
on Linux have always been kernel-level threads. LinuxThreads didn't comply fully with POSIX (threads in a same process had different pid's, signal handling was different than what POSIX requires, ...), so NPTL was created to fix those issues.There are libraries that implement user-level threads (e.g: GNU pth, the p is for Portable), but they don't use the POSIX thread API.