POSIX线程标准(POXIS Threads,缩写为Pthreads)是POSIX的线程标准,定义了一系列操作线程 的API。
POSIX标准,POSIX表示可移植操作系统接口,即Portable Operating System Interface of UNIX,缩写为POSIX。POSIX 标准定义了操作系统为应用程序提供的接口标准,是IEEE为要在各种 UNIX操作系统上运行的软件而定义的一系列 API 标准的总称,其正式称呼为IEEE 1003,而国际标 准名称为ISO/IEC 9945。
20世纪80年代,unix厂商视图增加一些新的、往往不兼容的特性使特们的程序体现差异化,这样 导致通用性降低,为阻止这一趋势,IEEE开始努力使unix进行标准化开发,后来由 Richard Stallman 命名为“Posix”,从而就得到了一系列标准,称作Posix标准。这套标准涵盖了很多方面,比如Unix系 统调用的C语言接口、shell程序和工具、线程及网络编程。
Linux上没有真正意义的线程,使用进程来模拟,属于用户级线程,位于libpthread共享库, 遵循POSIX标准。Linux最有名的两个线程库:LinuxThreads 和 NPTL。 Linux从内核2.6开始, 多线程已经使用NPTL技术。查看系统使用的线程库,getconf GNU_LIBPTHREAD_VERSION。
当前我使用的操作系统CentOS Linux release 7.4.1708,线程库查看如下: [root@host122 ~]# getconf GNU_LIBPTHREAD_VERSION NPTL 2.17
查看该系统支持的线程API:
[root@host122 ~]# man -k pthread pthread_atfork (3p) - register fork handlers pthread_attr_destroy (3) - initialize and destroy thread attributes object pthread_attr_getaffinity_np (3) - set/get CPU affinity attribute in thread attributes object pthread_attr_getdetachstate (3) - set/get detach state attribute in thread attributes object pthread_attr_getguardsize (3) - set/get guard size attribute in thread attributes object pthread_attr_getinheritsched (3) - set/get inherit-scheduler attribute in thread attributes object pthread_attr_getschedparam (3) - set/get scheduling parameter attributes in thread attributes object pthread_attr_getschedpolicy (3) - set/get scheduling policy attribute in thread attributes object pthread_attr_getscope (3) - set/get contention scope attribute in thread attributes object pthread_attr_getstack (3) - set/get stack attributes in thread attributes object pthread_attr_getstackaddr (3) - set/get stack address attribute in thread attributes object pthread_attr_getstacksize (3) - set/get stack size attribute in thread attributes object pthread_attr_init (3) - initialize and destroy thread attributes object pthread_attr_setaffinity_np (3) - set/get CPU affinity attribute in thread attributes object pthread_attr_setdetachstate (3) - set/get detach state attribute in thread attributes object pthread_attr_setguardsize (3) - set/get guard size attribute in thread attributes object pthread_attr_setinheritsched (3) - set/get inherit-scheduler attribute in thread attributes object pthread_attr_setschedparam (3) - set/get scheduling parameter attributes in thread attributes object pthread_attr_setschedpolicy (3) - set/get scheduling policy attribute in thread attributes object pthread_attr_setscope (3) - set/get contention scope attribute in thread attributes object pthread_attr_setstack (3) - set/get stack attributes in thread attributes object pthread_attr_setstackaddr (3) - set/get stack address attribute in thread attributes object pthread_attr_setstacksize (3) - set/get stack size attribute in thread attributes object pthread_barrierattr_destroy (3p) - destroy and initialize the barrier attributes object (ADVANCED REALTIME THREADS) pthread_barrierattr_getpshared (3p) - get and set the process-shared attribute of the barrier attributes object (ADVANCED REALTIME THREADS) pthread_barrierattr_init (3p) - destroy and initialize the barrier attributes object (ADVANCED REALTIME THREADS) pthread_barrierattr_setpshared (3p) - get and set the process-shared attribute of the barrier attributes object (ADVANCED REALTIME THREADS) pthread_barrier_destroy (3p) - destroy and initialize a barrier object (ADVANCED REALTIME THREADS) pthread_barrier_init (3p) - destroy and initialize a barrier object (ADVANCED REALTIME THREADS) pthread_barrier_wait (3p) - synchronize at a barrier (ADVANCED REALTIME THREADS) pthread_cancel (3) - send a cancellation request to a thread pthread_cleanup_pop (3) - push and pop thread cancellation clean-up handlers pthread_cleanup_pop_restore_np (3) - push and pop thread cancellation clean-up handlers while saving cancelability type pthread_cleanup_push (3) - push and pop thread cancellation clean-up handlers pthread_cleanup_push_defer_np (3) - push and pop thread cancellation clean-up handlers while saving cancelability type pthread_condattr_destroy (3p) - destroy and initialize the condition variable attributes object pthread_condattr_getclock (3p) - get and set the clock selection condition variable attribute (ADVANCED REALTIME) pthread_condattr_getpshared (3p) - get and set the process-shared condition variable attributes pthread_condattr_init (3p) - destroy and initialize the condition variable attributes object pthread_condattr_setclock (3p) - get and set the clock selection condition variable attribute (ADVANCED REALTIME) pthread_condattr_setpshared (3p) - get and set the process-shared condition variable attributes pthread_cond_broadcast (3p) - broadcast or signal a condition pthread_cond_destroy (3p) - destroy and initialize condition variables pthread_cond_init (3p) - destroy and initialize condition variables pthread_cond_signal (3p) - broadcast or signal a condition pthread_cond_timedwait (3p) - wait on a condition pthread_cond_wait (3p) - wait on a condition pthread_create (3) - create a new thread pthread_detach (3) - detach a thread pthread_equal (3) - compare thread IDs pthread_exit (3) - terminate calling thread pthread_getaffinity_np (3) - set/get CPU affinity of a thread pthread_getattr_np (3) - get attributes of created thread pthread_getconcurrency (3) - set/get the concurrency level pthread_getcpuclockid (3) - retrieve ID of a thread's CPU time clock pthread_getname_np (3) - set/get the name of a thread pthread_getschedparam (3) - set/get scheduling policy and parameters of a thread pthread_getspecific (3p) - thread-specific data management pthread.h (0p) - threads pthread_join (3) - join with a terminated thread pthread_key_create (3p) - thread-specific data key creation pthread_key_delete (3p) - thread-specific data key deletion pthread_kill (3) - send a signal to a thread pthread_kill_other_threads_np (3) - terminate all other threads in process pthread_mutexattr_destroy (3p) - destroy and initialize the mutex attributes object pthread_mutexattr_getprioceiling (3p) - get and set the prioceiling attribute of the mutex attributes object (REALTIME THREADS) pthread_mutexattr_getprotocol (3p) - get and set the protocol attribute of the mutex attributes object (REALTIME THREADS) pthread_mutexattr_getpshared (3p) - get and set the process-shared attribute pthread_mutexattr_getrobust (3p) - get and set the mutex robust attribute pthread_mutexattr_gettype (3p) - get and set the mutex type attribute pthread_mutexattr_init (3p) - destroy and initialize the mutex attributes object pthread_mutexattr_setprioceiling (3p) - get and set the prioceiling attribute of the mutex attributes object (REALTIME THREADS) pthread_mutexattr_setprotocol (3p) - get and set the protocol attribute of the mutex attributes object (REALTIME THREADS) pthread_mutexattr_setpshared (3p) - get and set the process-shared attribute pthread_mutexattr_setrobust (3p) - get and set the mutex robust attribute pthread_mutexattr_settype (3p) - get and set the mutex type attribute pthread_mutex_consistent (3p) - mark state protected by robust mutex as consistent pthread_mutex_destroy (3p) - destroy and initialize a mutex pthread_mutex_getprioceiling (3p) - get and set the priority ceiling of a mutex (REALTIME THREADS) pthread_mutex_init (3p) - destroy and initialize a mutex pthread_mutex_lock (3p) - lock and unlock a mutex pthread_mutex_setprioceiling (3p) - get and set the priority ceiling of a mutex (REALTIME THREADS) pthread_mutex_timedlock (3p) - lock a mutex (ADVANCED REALTIME) pthread_mutex_trylock (3p) - lock and unlock a mutex pthread_mutex_unlock (3p) - lock and unlock a mutex pthread_once (3p) - dynamic package initialization pthread_rwlockattr_destroy (3p) - destroy and initialize the read-write lock attributes object pthread_rwlockattr_getpshared (3p) - get and set the process-shared attribute of the read-write lock attributes object pthread_rwlockattr_init (3p) - destroy and initialize the read-write lock attributes object pthread_rwlockattr_setpshared (3p) - get and set the process-shared attribute of the read-write lock attributes object pthread_rwlock_destroy (3p) - destroy and initialize a read-write lock object pthread_rwlock_init (3p) - destroy and initialize a read-write lock object pthread_rwlock_rdlock (3p) - lock a read-write lock object for reading pthread_rwlock_timedrdlock (3p) - lock a read-write lock for reading pthread_rwlock_timedwrlock (3p) - lock a read-write lock for writing pthread_rwlock_tryrdlock (3p) - lock a read-write lock object for reading pthread_rwlock_trywrlock (3p) - lock a read-write lock object for writing pthread_rwlock_unlock (3p) - unlock a read-write lock object pthread_rwlock_wrlock (3p) - lock a read-write lock object for writing pthreads (7) - POSIX threads pthread_self (3) - obtain ID of the calling thread pthread_setaffinity_np (3) - set/get CPU affinity of a thread pthread_setcancelstate (3) - set cancelability state and type pthread_setcanceltype (3) - set cancelability state and type pthread_setconcurrency (3) - set/get the concurrency level pthread_setname_np (3) - set/get the name of a thread pthread_setschedparam (3) - set/get scheduling policy and parameters of a thread pthread_setschedprio (3) - set scheduling priority of a thread pthread_setspecific (3p) - thread-specific data management pthread_sigmask (3) - examine and change mask of blocked signals pthread_sigqueue (3) - queue a signal and data to a thread pthread_spin_destroy (3p) - destroy or initialize a spin lock object (ADVANCED REALTIME THREADS) pthread_spin_init (3p) - destroy or initialize a spin lock object (ADVANCED REALTIME THREADS) pthread_spin_lock (3p) - lock a spin lock object (ADVANCED REALTIME THREADS) pthread_spin_trylock (3p) - lock a spin lock object (ADVANCED REALTIME THREADS) pthread_spin_unlock (3p) - unlock a spin lock object (ADVANCED REALTIME THREADS) pthread_testcancel (3) - request delivery of any pending cancellation request pthread_timedjoin_np (3) - try to join with a terminated thread pthread_tryjoin_np (3) - try to join with a terminated thread pthread_yield (3) - yield the processor vfs_aio_pthread (8) - implement async I/O in Samba vfs using a pthread poolNPTL 源码 http://ftp.gnu.org/pub/gnu/glibc/glibc-2.9.tar.gz 或者 https://sourceforge.net/projects/nuttx/
线程取消涉及的函数如下:
设置取消状态: int pthread_setcancelstate(int state, int *oldstate); 设置取消类型 int pthread_setcanceltype(int type, int *oldtype); 当线程将退出作为对取消请求的响应时,取消类型允许线程控制它在什么地方退出,包括: PTHREAD_CANCEL_DEFERRED 时,线程只能在特定的几个取消点上响应取消请求,默认值 PTHREAD_CANCEL_ASYNCHRONOUS 时,线程在任何时候都可以响应取消请求pthread_cond_signal函数的作用是发送一个信号给另外一个正 在处于阻塞等待状态的线程,使其脱离阻塞状态,继续执行.如果没有线程处在阻塞等待状态, pthread_cond_signal也会成功返回。
使用pthread_cond_signal不会有“惊群现象”产生,他最多只给 一个线程发信号。假如有多个线程正在阻塞等待着这个条件变量的话,那么是根据各等待线程优先级 的高低确定哪个线程接收到信号开始继续执行。如果各线程优先级相同,则根据等待时间的长短来确 定哪个线程获得信号。
在LinuxThreads或者NPTL里面有两个队列,分别是cond_wait队 列和mutex_lock队列, cond_signal只是让线程从cond_wait队列移到mutex_lock队列,而不用 返回到用户空间,不会有性能的损耗。
该函数对应pthread_cond_wait, 同类型函数 pthread_cond_broadcast。
int pthread_cond_signal(pthread_cond_t *cond); 参数说明: cond : 线程条件变量分配用于标识进程中线程特定数据的键。 进程中并不能创建无限个的pthread_key_t变量。Linux中可以 通过PTHREAD_KEY_MAX(定义于limits.h文件中)或者系统调用sysconf(_SC_THREAD_KEYS_MAX) 来确定当前系统最多支持多少个键。Linux中默认是1024个键。
查看pthread_key_create.c 源码实现文件,可以更好的理解 pthread_key_create 函数,里面用了个for循环,实际内部创建的是一个数组。
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); 参数说明: key :线程特定数据的标识键 (*destructor)(void*) :线程结束时,系统将调用这个函数来释放绑定在这个键上的内存 对应的销毁函数 int pthread_key_delete (pthread_key_t key);设置变量value 与 pthread_key_t 类型的变量key进行关联。 pthread_getpecific和pthread_setspecific提供了在同一个线程中不同函数间共享数据即线程 存储的一种方法。 c++11 提供了一个新的数据类型 thread_local,该数据类型的变量在每个线程中都是一个副本, 进行修改等处理不影响其他线程内保存的该变量的值。
int pthread_setspecific(pthread_key_t key, const void *value);只执行1次的设置函数
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); pthread_once_t once_control = PTHREAD_ONCE_INIT;POSIX 线程详解(经典必看) https://www.cnblogs.com/sunminmin/p/4479952.html
GLIBC中NPTL线程实现代码阅读 https://blog.csdn.net/whuzm08/article/details/53537631
pthread_cond_wait 为什么需要传递 mutex 参数? https://www.zhihu.com/question/24116967