线程属性
线程属性pthread_attr_t
typedef struct
{
int detachstate; 线程分离状态
int schedpolicy; 线程调度策略
struct sched_param schedparam; 线程调度参数
int inheritsched; 线程调度继承性
int scope; 线程作用域
size_t guardsize; 线程栈末尾警戒缓冲区大小
int stackaddr_set;
void * stackaddr; 线程栈的位置
size_t stacksize; 线程栈的大小
} pthread_attr_t
对线程属性进行处理之前必须进行初始化,使用之后去初始化
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr)
int pthread_attr_destroy(pthread_attr_t *attr)
线程分离状态:
线程分离状态决定一个线程以怎样的方式终止
非分离状态终止的线程,可以被其他线程等待,pthread_join返回时,线程释放资源
分离状态的终止的线程,不能被其他线程等待,线程结束后直接释放资源
int pthread_attr_getdetachstate(pthread_attr_t *attr,int *detachstate)
int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate)
detachstate的值: PTHREAD_CREATE_DETACHED
PTHREAD_CREATE_JOINABLE
线程继承性:
继承性决定调度的参数是从创建的线程中继承还是使用schedpolicy和schedparam属性中设置的调度信息
int pthread_attr_getinheritsched( pthread_attr_t *attr,int *inheritsched)
int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched)
inheritsched: PTHREAD_INHERIT_SCHED,新线程将继承创建线程的调度策略和参数
PTHREAD_EXPLICIT_SCHED,使用schedpolicy和schedparam设置值
线程的调度策略:
SCHED_FIFO:允许一个线程运行直到更高优先级的线程准备好,或直到自愿阻塞自己
SCHED_RR:如果有一个SCHED_RR策略的线程运行超过了一段时间没有阻塞,而另外的SCHED_RR或SCHED_FIFO策略的线程已经准备好,运行的线程将被抢占
SCHED_RR或SCHED_FIFO策略的线程在等待一个互斥量或条件变量时,高优先级线程将首先被解除阻塞
int pthread_attr_getschedpolicy(pthread_attr_t *attr,int *schedpolicy)
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int schedpolicy)
线程的调度参数
struct sched_param
{
int sched_priority;
};
结构体sched_param的sched_priority成员代表优先权值
获取系统支持的最高、最低优先权值:
int sched_get_priority_max(int pri_max)
int sched_get_priority_min(int pri_min)
最高、最低优先权值将保存在pri_max和pri_min中,但这里参数并不是指针
注意:调度策略是非常复杂的事,轻易修改线程的优先级权值,可能导致死锁等问题
获取/设置线程的调度参数
int pthread_attr_getschedparam(pthread_attr_t *attr,struct sched_param *schedparam)
int pthread_attr_setschedparam(pthread_attr_t *attr,struct sched_param *schedparam)
线程的作用域:
作用域控制线程是否在进程内或在系统级上竞争资源
PTHREAD_SCOPE_PROCESS:进程内竞争资源
PTHREAD_SCOPE_SYSTEM:系统上竞争资源
int pthread_attr_getscope(pthread_attr_t *attr,int *scope)
int pthread_attr_setscope(pthread_attr_t *attr,int scope)
线程堆栈的大小:
int pthread_attr_getstacksize(pthread_attr_t *attr,size_t *stacksize)
int pthread_attr_setstacksize(pthread_attr_t *attr,size_t stacksize)
线程堆栈的地址:
int pthread_attr_getstackaddr(pthread_attr_t *attr,void **stackaddr)
int pthread_attr_setstackaddr(pthread_attr_t *attr,void *stackaddr)
合二为一:
int pthread_attr_getstack(pthread_attr_t *attr,void **stackaddr,size_t *stacksize)
int pthread_attr_setstack(pthread_attr_t *attr,void *stackaddr,size_t stacksize)
线程栈末尾的警戒缓冲区大小:
int pthread_attr_getguardsize(pthread_attr_t *attr,size_t *guardsize)
int pthread_attr_setguardsize(pthread_attr_t *attr,size_t guardsize)
线程同步对象属性
互斥量属性
pthread_mutexattr_t
初始化:
int pthread_mutexattr_init(pthread_mutexattr_t *attr)
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
进程共享属性:
PTHREAD_PROCESS_SHARED:从多个进程共享的区域中分配的互斥量可以用于这些进程的同步
PTHREAD_PROCESS_PRIVATE:默认属性,不支持进程共享
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr,int *pshared)
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,int pshared)
类型属性:
PTHREAD_MUTEX_NORMAL:标准类型,不做特殊的错误检查或死锁检测
PTHREAD_MUTEX_ERRORCHECK:提供错误检查(加锁再加锁、不占用解锁)
PTHREAD_MUTEX_RECURSICE:允许多次加锁,并提供错误检查(不占用解锁)
PTHREAD_MUTEX_DEFAULT:linux将该类型映射到PTHREAD_MUTEX_NORML
int pthread_mutexattr_gettype(pthread_mutexattr_t *attr,int *type)
int pthread_mutexattr_settype(pthread_mutexattr_t *attr,int type)
读写锁属性
pthread_rwlockattr_t
初始化:
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
进程共享属性:
int pthread_twlockattr_getpshared(pthread_rwlockattr_t *attr,int *pshared)
int pthread_rwlockattr_setpshated(pthread_rwlockattr_t *attr,int pshared)
条件变量属性:
pthread_contattr_t
初始化:
int pthread_condattr_init(pthread_condattr_t *attr)
int pthread_condattr_destroy(pthread_condattr_t *attr)
进程共享属性:
int pthread_condattr_getpshared(pthread_condattr_t *attr,int *pshared)
int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared)