Is Pthread library actually a user thread solution?

MultithreadingPthreadsKernel

Multithreading Problem Overview


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.

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.

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?

Multithreading Solutions


Solution 1 - Multithreading

@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

Solution 2 - Multithreading

In Linux, pthread is implemented as a lightweight process. Kernel (v2.6+) is actually implemented with NPTL. Let me quote the wiki content:

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

So pthread in linux kernel is actually implemented as kernel thread.

Solution 3 - Multithreading

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.

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.

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.

Solution 4 - Multithreading

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

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

> Q: It is available in Unix-like OS.

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

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.

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

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.

A: No. The kernel knows everything :)

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

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

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

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 Kernel Development, Robert Love][1]

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

"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. [1]: http://www.amazon.com/Linux-Kernel-Development-Robert-Love/dp/0672329468

Solution 5 - Multithreading

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

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

Now,pthreads is a specification and 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).

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

Solution 6 - Multithreading

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMengfei MurphyView Question on Stackoverflow
Solution 1 - Multithreadingpareshverma91View Answer on Stackoverflow
Solution 2 - MultithreadingJunji ZhiView Answer on Stackoverflow
Solution 3 - MultithreadingFInView Answer on Stackoverflow
Solution 4 - Multithreadingpaulsm4View Answer on Stackoverflow
Solution 5 - MultithreadingYoni KerenView Answer on Stackoverflow
Solution 6 - MultithreadingEser AygünView Answer on Stackoverflow