关于 Linux 的 Thread
通俗意义下, Linux 的 pthread 是用户级线程,采用的是 N-on-N 调度模型,即内核一个轻量级进程管理一个核外线程。
1 Linux Implementations of POSIX Threads [1]
Over time, two threading implementations have been provided by the GNU C library on Linux:
过去曾经有一段时间, Linux 下有两种 GNU C 的线程库实现存在,即是说,这之前只有 LinuxThreads 库存,之后到现在只有 NPTL 库存在。
1.1 LinuxThreads
This is the original Pthreads implementation.
Since glibc 2.4, this implementation is no longer supported.
在使用 LinuxThreads 构建库时,需要使用 -D_REENTRANT 编译时标志。这使得库线程是安全的。
LinuxThreads 的设计通常都可以很好地工作;但是在压力很大的应用程序中。
它的性能、可伸缩性和可用性都会存在问题。[ 2 ]
到 2005 年作者还在维护。
1.2 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.
NPTL 是 2.6 内核的默认配置,优于 LinuxThreads 。从 2002 年到现在一直还有更新。
1.3 结论, pthread_creat( ) 速度远优于 fork( ).
2 查看系统 threads 库版本的命令:
$getconf GNU_LIBPTHREAD_VERSION NPTL 2.1
3 查看系统 threads 库作者版本的命令:
$(ldd /bin/ls | grep libc.so | awk '{print $3}')| egrep -i 'threads|nptl
Native POSIX Threads Library by Ulrich Drepper et al
参考文档:
[1] http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library
[2] http://www.ibm.com/developerworks/linux/library/l-threading.html?S_TACT=105AGX52&S_CMP=cn-a-l
[3] http://www.kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html
[4] The Native POSIX Thread Library for Linux, Ulrich Drepper,Ingo Molnar,Red Hat, Inc.
[5] http://www.gnu.org/software/libc/manual/html_mono/libc.html