multithreading Pthread 库实际上是用户线程解决方案吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8639150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Pthread library actually a user thread solution?
提问by Mengfei Murphy
The title might not be clear enough because I don't know how to define my questions actually.
标题可能不够清楚,因为我实际上不知道如何定义我的问题。
I understand Pthread is a thread library meeting POSIX standard (about POSIX, see wikipedia: http://en.wikipedia.org/wiki/Posix). It is available in Unix-like OS.
我了解 Pthread 是一个符合 POSIX 标准的线程库(关于 POSIX,请参阅维基百科:http: //en.wikipedia.org/wiki/Posix)。它在类 Unix 操作系统中可用。
About thread, I read that there are three different models:
关于线程,我读到有三种不同的模型:
User level thread:the kernel does not know it. User himself creates/implements/destroy threads.
用户级线程:内核不知道。用户自己创建/实现/销毁线程。
Kernel level thread:kernel directly supports multiple threads of control in a process.
内核级线程:内核直接支持一个进程中的多线程控制。
Light weight process(LWP):scheduled by kernel but can be bounded with user threads.
轻量级进程(LWP):由内核调度,但可以与用户线程绑定。
Did you see my confusion? When I call pthread_create()
to create a thread, did I create a user level thread? I guess so. So can I say, Pthread offers a user level solution for threads? It can not manipulate kernel/LWP?
你看到我的困惑了吗?当我调用pthread_create()
创建线程时,是否创建了用户级线程?大概吧。那么我可以说,Pthread 为线程提供了用户级解决方案吗?它不能操作内核/LWP?
采纳答案by paulsm4
Q: I understand Pthread is a thread library meeting POSIX standard
问:我知道 Pthread 是一个符合 POSIX 标准的线程库
A: Yes. Actually, "Pthreads" stands for "Posix threads": http://en.wikipedia.org/wiki/Pthreads
答:是的。实际上,“Pthreads”代表“Posix 线程”:http: //en.wikipedia.org/wiki/Pthreads
Q: It is available in Unix-like OS.
问:它在类 Unix 操作系统中可用。
A: Actually, it's available for many different OSs ... including Windows, MacOS ... and, of course, Linux, BSD and Solaris.
答:实际上,它可用于许多不同的操作系统……包括 Windows、MacOS……当然还有 Linux、BSD 和 Solaris。
Q: About thread, I read that there are three different models
问:关于线程,我读到有三种不同的模型
Now you're getting fuzzy. "Threads" is a very generic term. There are many, many different models. And many, many different ways you can characterize and/or implement "threads". Including stuff like the Java threading model, or the Ada threading model.
现在你越来越糊涂了。“线程”是一个非常通用的术语。有很多很多不同的模型。您可以通过许多不同的方式来表征和/或实现“线程”。包括诸如 Java 线程模型或 Ada 线程模型之类的东西。
Q: When I call pthread_create() to create a thread, did I create a user level thread?
Q:调用pthread_create()创建线程时,是否创建了用户级线程?
A: Yes: Just about everything you do in user space is "protected" in your own, private "user space".
答:是的:几乎您在用户空间中所做的一切都在您自己的私有“用户空间”中受到“保护”。
Q: User level thread: the kernel does not know it.
Q:用户级线程:内核不知道。
A: No. The kernel knows everything:)
A: 不。内核知道一切:)
Q: Kernel level thread: kernel directly supports multiple threads of control in a process.
Q:内核级线程:内核直接支持一个进程中的多线程控制。
A: Yes, there is such a thing as "kernel threads".
A:是的,有“内核线程”这样的东西。
And, as it happens, Linux makes EXTENSIVE use of kernel threads. For example, every single process in a Linux system is a "kernel thread". And every user-created pthread is ALSO implemented as a new "kernel thread". As are "worker threads" (which are completely invisible to any user-level process).
而且,碰巧的是,Linux 广泛使用了内核线程。例如,Linux 系统中的每个进程都是一个“内核线程”。每个用户创建的 pthread 也被实现为一个新的“内核线程”。就像“工作线程”(对任何用户级进程完全不可见)一样。
But this is an advanced topic you do NOT need to understand in order to effectively use pthreads. Here's a great book that discussed this - and many other topics - in detail:
但这是一个高级主题,您无需了解即可有效使用 pthread。这是一本很棒的书,它详细讨论了这个 - 以及许多其他主题:
Linux Kernel Development, Robert Love
Remember: "Pthreads" is an interface. How it's implemented depends on the platform. Linux uses kernel threads; Windows uses Win32 threads, etc.
请记住:“Pthreads”是一个接口。它的实现方式取决于平台。Linux 使用内核线程;Windows 使用 Win32 线程等。
=========================================================================== ADDENDUM:
================================================== ========================== 附录:
Since people still seem to be hitting this old thread, I thought it would be useful to reference this post:
由于人们似乎仍然在打这个旧线程,我认为参考这篇文章会很有用:
https://stackoverflow.com/a/11255174/421195
Linux typically uses two implementations of pthreads: LinuxThreadsand Native POSIX Thread Library(NPTL), although the former is largely obsolete. Kernel from 2.6 provides NPTL, which provides much closer conformance to SUSv3, and perform better especially when there are many threads.
You can query the specific implementation of pthreads under shell using command:
getconf GNU_LIBPTHREAD_VERSION
You can also get a more detailed implementation difference in The Linux Programming Interface.
https://stackoverflow.com/a/11255174/421195
Linux 通常使用 pthread 的两种实现: LinuxThreads和Native POSIX Thread Library(NPTL),尽管前者在很大程度上已经过时了。2.6 的内核提供了 NPTL,它提供了更接近 SUSv3 的一致性,并且在有很多线程时表现更好。
可以使用命令查询shell下pthreads的具体实现:
getconf GNU_LIBPTHREAD_VERSION
您还可以在The Linux Programming Interface 中获得更详细的实现差异。
"Pthreads" is a library, based on the Posix standard. How a pthreads library is implemented will differ from platform to platform and library to library.
“Pthreads”是一个基于 Posix 标准的库。pthreads 库的实现方式因平台和库而异。
回答by pareshverma91
@paulsm4 I am doubtful about your comment that kernel knows every thing. In this particular context of user level threads, the kernel is unaware of the fact that such a thing is happening. A user level thread's scheduling is maintained by the user himself (via the interface provided by a library) and the kernel ends up allotting just a single kernel thread to the whole process. Kernel would treat the process as a single threaded and any blocking call by one of the threads would end up blocking all the threads of that process. Refer to http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/threads.htm
@paulsm4 我对您的评论表示怀疑,即内核知道一切。在用户级线程的这个特定上下文中,内核不知道这样的事情正在发生的事实。用户级线程的调度由用户自己维护(通过库提供的接口),内核最终只为整个进程分配一个内核线程。内核会将进程视为单线程,并且其中一个线程的任何阻塞调用最终都会阻塞该进程的所有线程。参考http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/threads.htm
回答by FIn
pthreads, per se, isn't really a threading library. pthreads is the interface which a specific threading library implements, using the concurrency resources available on that platform. So there's a pthreads implementation on linux, on bsd, on solaris, etc., and while the interface (the header files and the meaning of the calls) is the same, the implementation of each is different.
pthreads 本身并不是一个真正的线程库。pthreads 是特定线程库实现的接口,使用该平台上可用的并发资源。所以在linux、bsd、solaris等上都有pthreads的实现,虽然接口(头文件和调用的含义)是一样的,但每个的实现是不同的。
So what pthread_create actually does, in terms of kernel thread objects, varies between OSes and pthread library implementations. At a first approximation, you don't need to know (that's stuff that the pthread abstraction allows you to not need to know about). Eventually you might need to see "behind the curtain", but for most pthread users that's not necessary.
因此,就内核线程对象而言,pthread_create 的实际作用因操作系统和 pthread 库实现而异。乍一看,您不需要知道(这是 pthread 抽象允许您不需要知道的东西)。最终您可能需要看到“幕后”,但对于大多数 pthread 用户来说,这不是必需的。
If you want to know what a /specific/ pthread implementation does, on a specific OS, you'll need to clarify your question. What Solaris and Linux do, for example, is very different.
如果您想知道 /specific/ pthread 实现在特定操作系统上的作用,您需要澄清您的问题。例如,Solaris 和 Linux 所做的就大不相同。
回答by Junji Zhi
In Linux, pthread is implemented as a lightweight process. Kernel (v2.6+) is actually implemented with NPTL. Let me quote the wiki content:
在 Linux 中,pthread 被实现为轻量级进程。Kernel (v2.6+) 实际上是用NPTL实现的。让我引用维基内容:
NPTL is a so-called 1×1 threads library, in that threads created by the user (via the pthread_create() library function) are in 1-1 correspondence with schedulable entities in the kernel (tasks, in the Linux case). This is the simplest possible threading implementation.
NPTL 是所谓的 1×1 线程库,因为用户创建的线程(通过 pthread_create() 库函数)与内核中的可调度实体(Linux 中的任务)是一一对应的。这是最简单的线程实现。
So pthread in linux kernel is actually implemented as kernel thread.
所以linux内核中的pthread实际上是作为内核线程来实现的。
回答by Yoni Keren
I find previous answers not as satisfying or clear as I would have liked so here goes:
我发现以前的答案并不像我希望的那样令人满意或清晰,所以这里是:
When you call
你打电话时
pthread_create(...)
you always create a new user-level thread. And assuming that there is OS, there is always one or more kernel thread...but let's dive deeper:
你总是创建一个新的用户级线程。假设有操作系统,总有一个或多个内核线程……但让我们更深入地研究:
According to "Operating system concepts" 10th edition,the actual classification we should be looking at (when it comes to thread libraries) is how the user level threads are mapped onto kernel threads (and that's what the question really meant).
根据“操作系统概念”第 10 版,我们应该查看的实际分类(当涉及线程库时)是用户级线程如何映射到内核线程(这就是问题的真正含义)。
The models are one to one(each user-level thread within a single process is mapped to a different kernel thread),many to one(the thread library is "user level" so all of the different threads within a single process are mapped to a single kernel thread,and the threads data structures, context switch etc are dealt with at user level and not by the OS [meaning that if a thread blocks on some I/O call, the entire process might potentially block]), and many to many(something in between,obviously the number of user-level threads is greater or equal to the number of kernel threads it is being mapped onto).
模型是一对一的(单个进程中的每个用户级线程都映射到不同的内核线程),多对一(线程库是“用户级”,因此单个进程中的所有不同线程都映射到单个内核线程,并且线程数据结构、上下文切换等是在用户级别处理的,而不是由操作系统处理 [这意味着如果某个线程在某些 I/O 调用上阻塞,整个进程可能会阻塞]),以及许多到许多(介于两者之间,显然用户级线程的数量大于或等于它被映射到的内核线程的数量)。
Now,pthreads is a specificationand not an implementation, and the implementation does depend on the OS to which it is written. It could be any one of those models (notice that "many to many" is very flexible).
现在,pthreads 是一个规范而不是一个实现,并且实现依赖于它被写入的操作系统。它可以是这些模型中的任何一种(请注意,“多对多”非常灵活)。
So,as an example,on Linux and Windows (the most popular OSs for years now,where the model is "one to one") the implementation is "one to one".
因此,举个例子,在 Linux 和 Windows(多年来最流行的操作系统,模型是“一对一”)上,实现是“一对一”。
回答by Eser Aygün
Pthreads is just a standardized interface for threading libraries. Whether an OS thread or a lightweight thread is created depends on the library you use. Nevertheless, my first guest would be that your threads are “real” OS-level threads.
Pthreads 只是线程库的标准化接口。是创建 OS 线程还是轻量级线程取决于您使用的库。尽管如此,我的第一个客人会认为您的线程是“真正的”操作系统级线程。