31

What is the basic difference between NPTL and POSIX threads? How have these two evolved?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Whoami
  • 13,930
  • 19
  • 84
  • 140

3 Answers3

40

POSIX threads (pthread) is not an implementation, it is a API specification (a standard, on paper, in english) of several functions whose name starts with pthread_ and which are defined in <pthread.h> header. POSIX is also a set of specifications.

NPTL is now inside GNU Libc on Linux and is (or at least tries very hard to be) an implementation of POSIX threads. It is a bunch of source and binary code on your Linux system. An application compiled with gcc -pthread and linked with -pthread uses NPTL code on Linux today.

addenda

There exist alternative implementations of pthread-s: on Linux, the MUSL Libc aims to be Posix compliant (which means having pthreads); on other Posix systems (AIX, Solaris, ...) you also have pthreads (but they are not NPTL or Glibc).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • So `-lpthread` and `-pthread` both use the NPTL implementation? – Anish Ramaswamy Feb 06 '14 at 03:53
  • Yes, on most Linux systems (but you could install MUSL libc) – Basile Starynkevitch Feb 06 '14 at 05:50
  • @BasileStarynkevitch, i am trying to figure out how musl doesn't break the build of code that links with a "-lpthread". I am trying to build our code which makes light use of pthreads, mutex, and cond against musl. It builds and links fine, but the thread behavior is truly odd. I looked at the configure script for musl but there does not seem to be any special switches for pthread. – user3342339 Feb 01 '17 at 00:27
9

"POSIX threads" is a 'standard', defining an API for threading. i.e. it states that functions such as pthread_exit () etc, should exist in the system, and describes how they should behave. All POSIX compliant operating systems implement POSIX threads in their own way.

NPTL is a bunch of features that enables "Linux" (the kernel) to efficiently implement "POSIX threads" (the standard).

You can read more about NPTL and how it came about here

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
4

I guess your best source of information is starting on Wikipedia and following the links from there.

There is really no difference: NPTL is just the current Linux implementation of POSIX threads, you still use the pthread_* family of functions. Earlier in Linux history, a dedicated library called libpthreads was used. NPTL appeared for 2.6+ kernels circa 2003, see the link above for more details.

[BTW: NPTL == Native Posix Threads Library]

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thanks for the reply. So, u mean to say NPTL and pthread both are using same API, but implementation is different ? – Whoami Dec 20 '11 at 13:30
  • Yes, exactly. NPTL makes use of the more advanced facilities available for task creation introduced with Linux kernel 2.6.+. – fge Dec 20 '11 at 13:31
  • So, Do We have two set of libraries one for NPTL and another one is for pthread ?.. IF so, can i how can i link to NPTL library to my program?. – Whoami Dec 20 '11 at 13:35
  • Nonononono. NPTL has been the standard POSIX threads library for a very long time now. At the time of the switch (more than 7 years ago), yes, you'd have had to choose, but now it is standard. As the Wikipedia entry says, it is now standard in glibc, and has been for a long time. – fge Dec 20 '11 at 13:38
  • 4
    pthread is NOT the same as NPTL. pthread is a Standard. NPTL is how Linux and GNU Libc implement it. If you make calls to pthread_* functions and include pthread.h in your program, and compile and run it on a fairly recent GNU/Linux machine, it will inherently use NPTL. POSIX threads are a feature provided by every POSIX compliant Operating System. – ArjunShankar Dec 20 '11 at 13:40