Understanding Pthreads Understanding Pthreads multithreading multithreading

Understanding Pthreads


I get the same results of the book with linux that contains the libc libuClibc-0.9.30.1.so (1).

root@OpenWrt:~# ./testmain thread pid is 1151child thread pid is 1153

and I tried to run this program with a linux that contains the libc from ubuntu libc6 (2)

$ ./testmain thread pid is 2609child thread pid is 2609

The libc (1) use linuxthreads implementation of pthread

And the libc (2) use NPTL ("Native posix thread library") implementation of pthread

According to the linuxthreads FAQ (in J.3 answer):

each thread is really a distinct process with a distinct PID, and signals sent to the PID of a thread can only be handled by that thread

So in the old libc which use linuxthreads implementation, each thread has its distinct PID

In the new libc version which use NPTL implementation, all threads has the same PID of the main process.

The NPTL was developed by redhat team. and according to the redhat NPTL document: One of the problems which are solved in the NPTL implementation is:

(Chapter: Problems with the Existing Implementation, page5)

Each thread having a different process ID causes compatibility problems with other POSIX thread implementations. This is in part a moot point since signals can'tbe used very well but is still noticeable


And that explain your issue.

You are using the new libc version that contains the NPTL ("Native posix thread library") implementation of pthread

And the Book use an old version of libc that contains linuxthreads implementation of pthread


The text you're working from is very old (2001). Older versions of Linux implemented threads as separate processes with a shared address space. Each thread had a separate pid. However this thread model was not POSIX compliant and had a number of portability problems.

Starting somewhere around 2.6, Linux switched to the "Native POSIX Thread Library" (NPTL). In this implementation, threads do not get their own PIDs.


All threads created by a process belong to this one process by defintion. To get this one process' process-id use getpid(), no matter from which of the process' threads.

The author of the document linked is correct, that under Linux (p)threads are implemented as distinct processes sharing the same address space as the one process they belong to. The latter however is not reflected by getpid(), the author of the linked document is wrong with this assumption.

To get the process-id of the distinct process created for a single thread use (the Linux specific) gettid()*1.


*1: Please note that the glibc does not provide a wrapper to this function call. Use syscall() to invoke it.