發表文章

目前顯示的是 三月, 2015的文章

pthread_create 會多建立一個 thread 的原因

之前在執行某個客戶的案子時,被問到一個問題:「你說你們程式開了兩個thread,那為什麼透過 ps 指令觀察到的個數卻是 4 個呢?」(ps 的指令為 ps -L,可以透過 PID 和 LWP 這兩個欄位看出來),一時之間不知道怎麼回答。之後由同事 York 在 man pthreads找到了下面的答案。

 Linux implementations of POSIX threads
       Over time, two threading implementations have been provided by the
       GNU C library on Linux:

LinuxThreads
              This is the original Pthreads implementation.  Since glibc
              2.4, this implementation is no longer supported.

NPTL (Native POSIX Threads Library)
              This is the modern Pthreads implementation.  By comparison
              with LinuxThreads, NPTL provides closer conformance to the
              requirements of the POSIX.1 specification and better
              performance when creating large numbers of threads.  NPTL is
              available since glibc 2.3.2, and requires features that are
              present in the Linux 2.6 kernel.

從上面我們可以知道,Linux 在 pthread 上的實作方成兩種,LinuxThreads 和 NPTL,其中 NPTL 是比較新的實作方式。而在 LinuxThreads 的章節則提到了:

In addition to the m…