Pthread库实际上是用户线程解决方案吗? [英] Is Pthread library actually a user thread solution?

查看:240
本文介绍了Pthread库实际上是用户线程解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能不够清晰,因为我实际上不知道如何定义我的问题.

The title might not be clear enough because I don't know how to define my questions actually.

我了解Pthread是符合POSIX标准的线程库(关于POSIX,请参见维基百科: http://en .wikipedia.org/wiki/Posix ).在类似Unix的操作系统中可用.

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.

关于线程,我读到有三种不同的模型:

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.

轻量级进程(LWP):由内核调度,但可以与用户线程绑定.

Light weight process(LWP): scheduled by kernel but can be bounded with user threads.

您看到我的困惑了吗?当我调用pthread_create()创建线程时,是否创建了用户级线程?大概吧.所以我可以说,Pthread为线程提供了用户级别的解决方案吗?它不能操纵内核/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是符合POSIX标准的线程库

Q: I understand Pthread is a thread library meeting POSIX standard

A:是的.实际上,"Pthreads"代表"Posix线程": http://en.wikipedia.org/wiki/Pthreads

A: Yes. Actually, "Pthreads" stands for "Posix threads": http://en.wikipedia.org/wiki/Pthreads

问:在类似Unix的操作系统中可用.

Q: It is available in Unix-like OS.

A:实际上,它可用于许多不同的操作系统,包括Windows,MacOS ...,当然还有Linux,BSD和Solaris.

A: Actually, it's available for many different OSs ... including Windows, MacOS ... and, of course, Linux, BSD and Solaris.

问:关于线程,我读到有三种不同的模型

Q: About thread, I read that there are three different models

现在,您变得越来越模糊. 线程"是一个非常通用的术语.有很多很多不同的模型.以及许多可以用来表征和/或实现线程"的不同方式.包括Java线程模型或Ada线程模型之类的东西.

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.

问:当我调用pthread_create()创建线程时,是否创建了一个线程? 用户级线程?

Q: When I call pthread_create() to create a thread, did I create a user level thread?

A:是的:您在用户空间中所做的一切几乎都在您自己的私有用户空间"中受到保护".

A: Yes: Just about everything you do in user space is "protected" in your own, private "user space".

Q:用户级线程:内核不知道.

Q: User level thread: the kernel does not know it.

A:否.内核知道一切:)

A: No. The kernel knows everything :)

问:内核级线程:内核直接支持以下多个线程: 一个过程中的控制.

Q: Kernel level thread: kernel directly supports multiple threads of control in a process.

A:是的,有诸如内核线程"之类的东西.

A: Yes, there is such a thing as "kernel threads".

而且,碰巧的是,Linux大量使用了内核线程.例如,Linux系统中的每个进程都是一个内核线程".而且,每个用户创建的pthread都将实现为新的内核线程".和工作线程"一样(对于任何用户级进程而言都是完全不可见的).

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).

但这是您无需了解的高级主题即可有效地使用pthreads.这是一本很棒的书,详细讨论了这个主题以及许多其他主题:

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:

Linux内核开发,罗伯特·洛夫(Robert Love)

请记住:"Pthreads"是一个接口.如何实现取决于平台. Linux使用内核线程; Windows使用Win32线程等.

Remember: "Pthreads" is an interface. How it's implemented depends on the platform. Linux uses kernel threads; Windows uses Win32 threads, etc.

================================================ =========================== 附录:

=========================================================================== 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通常使用两种pthread实现: LinuxThreads

Linux typically uses two implementations of pthreads: LinuxThreads and 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.

您可以查询 在shell下使用命令pthreads的特定实现:

You can query the specific implementation of pthreads under shell using command:

getconf GNU_LIBPTHREAD_VERSION

您还可以在 Linux编程接口.

You can also get a more detailed implementation difference in The Linux Programming Interface.

"Pthreads"是基于Posix标准的. pthreads库的实现方式因平台而异,因库而异.

"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.

这篇关于Pthread库实际上是用户线程解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆