NPTL vs PThread

NPTL vs PThread

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.

Native POSIX Thread Library

The Native POSIX Thread Library (NPTL) is an implementation of the POSIX Threads specification for the Linux operating system.

History

Before the 2.6 version of the Linux kernel, processes were the schedulable entities, and there were no special facilities for threads. However, it did have a system call — clone — which creates a copy of the calling process where the copy shares the address space of the caller. The LinuxThreads project used this system call to provide kernel-level threads (most of the previous thread implementations in Linux worked entirely in userland). Unfortunately, it only partially complied with POSIX, particularly in the areas of signal handling, scheduling, and inter-process synchronization primitives.

To improve upon LinuxThreads, it was clear that some kernel support and a new threading library would be required. Two competing projects were started to address the requirement: NGPT (Next Generation POSIX Threads) worked on by a team which included developers from IBM, and NPTL by developers at Red Hat. The NGPT team collaborated closely with the NPTL team and combined the best features of both implementations into NPTL. The NGPT project was subsequently abandoned in mid-2003 after merging its best features into NPTL.

NPTL was first released in Red Hat Linux 9. Old-style Linux POSIX threading is known for having trouble with threads that refuse to yield to the system occasionally, because it does not take the opportunity to preempt them when it arises, something that Windows was known to do better at the time. Red Hat claimed that NPTL fixed this problem in an article on the Java website about Java on Red Hat Linux 9.

NPTL has been part of Red Hat Enterprise Linux since version 3, and in the Linux kernel since version 2.6. It is now a fully integrated part of the GNU C Library.

There exists a tracing tool for NPTL, called POSIX Thread Trace Tool (PTT). And an Open POSIX Test Suite (OPTS) was written for testing the NPTL library against the POSIX standard.

Design

NPTL uses a similar approach to LinuxThreads, in that the primary abstraction known by the kernel is still a process, and new threads are created with the clone() system call (called from the NPTL library). However, NPTL requires specialized kernel support to implement (for example) the contended case of synchronisation primitives which might require threads to sleep and wake again. The primitive used for this is known as a futex.

NPTL is a so-called 1×1 threads library, in that threads created by the user (via the pthread_create() library function) are in 1-1 correspondence with schedulable entities in the kernel (tasks, in the Linux case). This is the simplest possible threading implementation.

An alternative to NPTL‘s 1×1 model is the m×n model.

时间: 2024-10-07 13:39:45

NPTL vs PThread的相关文章

NPTL分析之线程的创建

NPTL(NativePosix Thread Library) NPTL包括pthread线程库以及配套的同步方法,我们这里暂时只讲pthread线程库的实现. 1. NPTL的起源 在NPTL之前,linux的线程库是LinuxThreads,该库部分实现了Posixthreads的规范.其主要特点是线程的调度在用户态完成,且由一个管理线程来调度.相应的,其缺点也来自于这种架构,导致管理线程带来了很大的任务切换的开销.另外一个问题是LinuxThreads对信号的处理不满足Posixthre

详细分析contrex-A9的汇编代码__switch_to(进程切换)

//函数原型:版本linux-3.0.8 struct task_struct *__switch_to(structtask_struct *, struct thread_info *, struct thread_info *); #define switch_to(prev,next,last)                                       \ do {                                                     

具体分析contrex-A9的汇编代码__switch_to(进程切换)

//函数原型:版本号linux-3.0.8 struct task_struct *__switch_to(structtask_struct *, struct thread_info *, struct thread_info *); #define switch_to(prev,next,last)                                       \ do {                                                    

正确使用pthread_create,防止内存泄漏

近日,听说pthread_create会造成内存泄漏,觉得不可思议,因此对posix(nptl)的线程创建和销毁进行了分析. 分析结果:如果使用不当,确实会造成内存泄漏. 产生根源:pthread_create默认创建的线程是非detached的. 预防方式:要么创建detached的线程,要么线程线程的start_routine结束之前detached,要么join 分析过程如下:   1.查看pthread_create源代码,核心代码如下(nptl/pthread_create.c): 点

安装glibc十分麻烦,还要检查版本的问题

[[email protected] opt]# ./glibc-2.6.1/configure --prefix=/usr/local/glibc-2.6.1/ checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu configure: running configure fragment for add-on nptl check

Glibc-2.3.4编译

$tar xf Glibc2.3.4.tar.bz2 $mkdir build_glibc $cd build_glibc ../glibc-2.3.4/configure --prefix=/tools \ --disable-profile --enable-add-ons \ --enable-kernel=2.6.0 --with-binutils=/tools/bin \ --without-gd --with-headers=/tools/include \ --without-se

GLib库安装与简析

GLib是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库, 它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏, 可移植的封装和一些运行时机能,如事件循环.线程.动态调用.对象系统等的API. 它能够在类UNIX的操作系统平台(如LINUX, HP-UNIX等),WINDOWS,OS2和BeOS等操作系统台上运行. 一.GLib在CentOS上的安装 检查系统当前的版本 # rpm -qi glibc Name        : glibc

POSIX多线程

全文共分四部分: POSIX多线程—概述    POSIX多线程—异步编程举例    POSIX多线程—线程基本概念    POSIX多线程—互斥量概述 POSIX多线程—概述 Content 1. 基础概念 2. 线程安全 3. 可重入 4. 并发系统基本功能 1. 基础概念 线程 进程里执行代码的部分: 包含一系列机器指令所必须的机器状态,包括当前指令位置(一般为PC寄存器).栈顶指针SP.通用寄存器.地址和数据寄存器等. 线程不包括进程中的其他数据,如地址空间和文件描述符: 进程 线程加上

life of a NPTL pthread

这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=91 March 7, 2013 life of a NPTL pthread Filed under: concurrency,linux,posix — Tags: NPTL, pthread — Raison @ 12:52 pm (Original Work by Peixu Zhu) NPTL pthread is the default pthread imple