thread created by pthread in Linux belongs to ULT or KLT?

16 Views Asked by At

Whether a thread created using Linux's pthread library is a user-level thread or a kernel-level thread?

I hope to get a professional answer to this question. Because I've heard that different versions of the Linux kernel as well as different compiler versions can have an impact on the answer to the question.

1

There are 1 best solutions below

0
John Bollinger On

Whether a thread created using Linux's pthread library is a user-level thread or a kernel-level thread?

If it's created using a program or library hosted on top of the OS, then it is a user-level thread.

[...] I've heard that different versions of the Linux kernel as well as different compiler versions can have an impact on the answer to the question.

No, they don't. Threads that run in userspace are user-level threads. Threads that run in the kernel are kernel-level threads. Unless you are writing kernel code, user-level threads are the only kind you can directly start or interact with. And if you are writing kernel code, then you don't have pthreads or other libraries to draw on, just the kernel itself.

You may be confused about historically there having been multiple Pthreads implementations for Linux, primarily:

  • LinuxThreads, the original Pthreads implementation for Linux. It runs on kernel 2.4+ (and perhaps earlier), and may still be running on a few legacy systems.
  • the Native Posix Thread Library (NPTL), the prevailing replacement for LinuxThreads. It is widely considered superior to LinuxThreads in several important ways, but it does require facilities introduced in kernel 2.6.

Both those implementations implement user-level threads, as entities that are scheduled by the kernel. That they are scheduled by the kernel does not make such threads kernel-level.

More generally, not all threading implementations provide the Pthreads API. There have been some that managed threads entirely in userspace (inside processes that are overall scheduled by the kernel). Such implementations are sometimes called "green threads". I'm not aware of any Linux Pthreads implementation using this approach, but if there were, it would certainly provide user-level threads.

Linux does have kernel-level threads, but your user-level programs will not see them or interact directly with them.