pthreads(5)
名称
threads , pthreads - POSIX pthread 和 Solaris 线程概念
用法概要
POSIX
cc –mt [ flag... ] file... [ library... ]
#include <pthread.h>
Solaris
cc –mt [ flag... ] file... [ library... ]
#include <sched.h>
#include <thread.h>
描述
POSIX 和 Solaris 线程在 libc(3LIB) 中均具有各自的实现。这两个实现可交互操作,其功能相似并可在同一应用程序中使用。只有 POSIX 线程保证可完全移植到符合 POSIX 的其他环境。POSIX 和 Solaris 线程需要不同的源,包括文件和链接系统信息库。请参见“用法概要”部分。
相似之处
大部分 POSIX 和 Solaris 线程函数互相对应。POSIX 函数名称具有 “pthread” 前缀(信号名称除外)。相似的 POSIX 和 Solaris 函数的函数名称也具有相似的结尾。通常,相似的 POSIX 和 Solaris 函数的参数数目和用法相同。
不同之处
POSIX pthread 和 Solaris 线程的不同之处在于:
POSIX 线程的可移植性更好。
POSIX 线程根据可配置的属性对象为每个线程建立特性。
POSIX pthread 实现了线程取消。
POSIX pthread 强制执行调度算法。
POSIX pthread 允许使用 fork(2) 调用的清理处理程序。
可暂停和继续执行 Solaris 线程。
Solaris 线程实现守护进程线程,进程不会等待其终止。
函数比较
函数比较
下表对 POSIX pthread 和 Solaris 线程函数进行了比较。当 POSIX pthread 或 Solaris 线程中没有可比较的接口时,列中会显示连字符 (–)。
与创建有关的函数
POSIX
|
Solaris
|
pthread_create()
|
thr_create()
|
pthread_attr_init()
|
–
|
pthread_attr_setdetachstate()
|
–
|
pthread_attr_getdetachstate()
|
–
|
pthread_attr_setinheritsched()
|
–
|
pthread_attr_getinheritsched()
|
–
|
pthread_attr_setschedparam()
|
–
|
pthread_attr_getschedparam()
|
–
|
pthread_attr_setschedpolicy()
|
–
|
pthread_attr_getschedpolicy()
|
–
|
pthread_attr_setscope()
|
–
|
pthread_attr_getscope()
|
–
|
pthread_attr_setstackaddr()
|
–
|
pthread_attr_getstackaddr()
|
–
|
pthread_attr_setstacksize()
|
–
|
pthread_attr_getstacksize()
|
–
|
pthread_attr_getguardsize()
|
–
|
pthread_attr_setguardsize()
|
–
|
pthread_attr_destroy()
|
–
|
–
|
thr_min_stack()
|
|
与退出有关的函数
POSIX
|
Solaris
|
pthread_exit()
|
thr_exit()
|
pthread_join()
|
thr_join()
|
pthread_detach()
|
–
|
|
与线程特定数据有关的函数
POSIX
|
Solaris
|
pthread_key_create()
|
thr_keycreate()
|
pthread_setspecific()
|
thr_setspecific()
|
pthread_getspecific()
|
thr_getspecific()
|
pthread_key_delete()
|
–
|
|
与信号有关的函数
POSIX
|
Solaris
|
pthread_sigmask()
|
thr_sigsetmask()
|
pthread_kill()
|
thr_kill()
|
|
与 ID 有关的函数
POSIX
|
Solaris
|
pthread_self()
|
thr_self()
|
pthread_equal()
|
–
|
–
|
thr_main()
|
|
与调度有关的函数
POSIX
|
Solaris
|
–
|
thr_yield()
|
–
|
thr_suspend()
|
–
|
thr_continue()
|
pthread_setconcurrency()
|
thr_setconcurrency()
|
pthread_getconcurrency()
|
thr_getconcurrency()
|
pthread_setschedparam()
|
thr_setprio()
|
pthread_setschedprio()
|
thr_setprio()
|
pthread_getschedparam()
|
thr_getprio()
|
|
与取消有关的函数
POSIX
|
Solaris
|
pthread_cancel()
|
–
|
pthread_setcancelstate()
|
–
|
pthread_setcanceltype()
|
–
|
pthread_testcancel()
|
–
|
pthread_cleanup_pop()
|
–
|
pthread_cleanup_push()
|
–
|
|
与互斥锁有关的函数
POSIX
|
Solaris
|
pthread_mutex_init()
|
mutex_init()
|
pthread_mutexattr_init()
|
–
|
pthread_mutexattr_setpshared()
|
–
|
pthread_mutexattr_getpshared()
|
–
|
pthread_mutexattr_setprotocol()
|
–
|
pthread_mutexattr_getprotocol()
|
–
|
pthread_mutexattr_setprioceiling()
|
–
|
pthread_mutexattr_getprioceiling()
|
–
|
pthread_mutexattr_settype()
|
–
|
pthread_mutexattr_gettype()
|
–
|
pthread_mutexattr_setrobust()
|
–
|
pthread_mutexattr_getrobust()
|
–
|
pthread_mutexattr_destroy()
|
–
|
pthread_mutex_setprioceiling()
|
–
|
pthread_mutex_getprioceiling()
|
–
|
pthread_mutex_lock()
|
mutex_lock()
|
pthread_mutex_trylock()
|
mutex_trylock()
|
pthread_mutex_unlock()
|
mutex_unlock()
|
pthread_mutex_destroy()
|
mutex_destroy()
|
|
与条件变量有关的函数
POSIX
|
Solaris
|
pthread_cond_init()
|
cond_init()
|
pthread_condattr_init()
|
–
|
pthread_condattr_setpshared()
|
–
|
pthread_condattr_getpshared()
|
–
|
pthread_condattr_destroy()
|
–
|
pthread_cond_wait()
|
cond_wait()
|
pthread_cond_timedwait()
|
cond_timedwait()
|
pthread_cond_signal()
|
cond_signal()
|
pthread_cond_broadcast()
|
cond_broadcast()
|
pthread_cond_destroy()
|
cond_destroy()
|
|
与读取器/写入器锁定有关的函数
POSIX
|
Solaris
|
pthread_rwlock_init()
|
rwlock_init()
|
pthread_rwlock_rdlock()
|
rw_rdlock()
|
pthread_rwlock_tryrdlock()
|
rw_tryrdlock()
|
pthread_rwlock_wrlock()
|
rw_wrlock()
|
pthread_rwlock_trywrlock()
|
rw_trywrlock()
|
pthread_rwlock_unlock()
|
rw_unlock()
|
pthread_rwlock_destroy()
|
rwlock_destroy()
|
pthread_rwlockattr_init()
|
–
|
pthread_rwlockattr_destroy()
|
–
|
pthread_rwlockattr_getpshared()
|
–
|
pthread_rwlockattr_setpshared()
|
–
|
|
与信号有关的函数
POSIX
|
Solaris
|
sem_init()
|
sema_init()
|
sem_open()
|
–
|
sem_close()
|
–
|
sem_wait()
|
sema_wait()
|
sem_trywait()
|
sema_trywait()
|
sem_post()
|
sema_post()
|
sem_getvalue()
|
–
|
sem_unlink()
|
–
|
sem_destroy()
|
sema_destroy()
|
|
与 fork( ) 清理有关的函数
POSIX
|
Solaris
|
pthread_atfork()
|
–
|
|
与限制有关的函数
POSIX
|
Solaris
|
pthread_once()
|
–
|
|
与调试有关的函数
POSIX
|
Solaris
|
–
|
thr_stksegment()
|
|
锁定
锁定
同步
多线程行为是异步的,因此针对并发和并行处理进行了优化。由于线程(始终来自同一进程,有时来自多个进程)相互之间共享全局数据,因此无法保证它们对任何时间点的共享数据具有独占访问权限。要获得对共享数据的独占访问权限,需要在线程之间进行同步。POSIX 和 Solaris 均实现了四种同步机制:互斥锁、条件变量、读取器/写入器锁定(优化的频繁读、少量写互斥锁)以及信号。
同步多个线程会大大降低线程并发性。同步的粒度越粗,即锁定的代码块越大,并发性就越小。
MT fork()
如果线程程序调用 fork(2),它会隐式调用只复制调用线程的 fork1(2)。如果整个进程中还有任何待处理的互斥锁,则应用程序应在调用 fork() 之前调用 pthread_atfork(3C) 等待并获取这些互斥锁。
调度
调度
POSIX 线程
Solaris 支持以下三种 POSIX 调度策略:
- SCHED_OTHER
传统分时调度策略。该策略基于分时 (timesharing, TS) 调度类。
- SCHED_FIFO
先入先出调度策略。如果不被更高优先级抢占,根据此策略调度的线程将会继续执行直至完成。此类线程属于实时 (real-time, RT) 调度类。调用进程必须在其有效集中声明 {PRIV_PROC_PRIOCNTL} 特权。
- SCHED_RR
循环调度策略。如果不被更高优先级抢占,根据此策略调度的线程将在系统确定的时段内执行。此类线程属于实时 (real-time, RT) 调度类,调用进程必须在其有效集中声明 {PRIV_PROC_PRIOCNTL} 特权。
除了上述 POSIX 指定的调度策略以外,Solaris 还支持以下调度策略:
- SCHED_IA
根据 priocntl(2) 中所述的交互式类 (Inter-Active Class, IA) 策略调度线程。
- SCHED_FSS
根据 priocntl(2) 中所述的公平份额类 (Fair-Share Class, FSS) 策略调度线程。
- SCHED_FX
根据 priocntl(2) 中所述的固定优先级类 (Fixed-Priority Class, FSS) 策略调度线程。
Solaris 线程
唯一受支持的调度策略是 SCHED_OTHER,即基于 TS 调度类的分时。
错误
在多线程应用程序中,当有其他线程调用 forkall(2) 时,将会从阻止的系统调用返回 EINTR。
用法
–mt 编译器选项
–mt 编译器选项会编译和链接多线程代码。它使用 −D_REENTRANT 编译源文件并适当扩大支持系统信息库集。
属性
有关下列属性的说明,请参见 attributes(5):
|
|
MT 级别
|
MT-Safe、Fork 1-Safe
|
|
另请参见
crle(1)、fork(2)、priocntl(2)、libpthread(3LIB)、librt(3LIB)、libthread(3LIB)、pthread_atfork(3C)、pthread_create(3C)、attributes(5)、privileges(5)、standards(5)
Oracle Solaris 11.2 链接程序和库指南