第12章线程控制总结
1 线程属性
线程属性:pthread_attr_t int pthread_attr_init(pthread_attr_t *attr) int pthread_attr_destroy(pthread_attr_t *attr)
pthread_attr_init 为属性对象分配动态内存空间,pthread_attr_destroy将释放这些内存空间,并使用无效的值初始化属性对象,该属性对象如果再次被pthread_create调用将出错
线程属性:
typedef struct
{
int detachstate; 线程分离状态
int schedpolicy; 线程调度策略
struct schedparam schedparam; 线程调度参数
int inheritsched; 线程继承性
int scope; 线程作用域
size_t guardsize; 线程栈末尾警戒缓冲区大小
int stackaddr_set;
void * stackaddr; 线程栈的位置
size_t stacksize; 线程栈的大小
} pthread_attr_t;
通过pthread_attr_getxxxx(pthread_attr_t *attr,int *val)形式的函数获取具体某个属性的值
通过pthread_attr_setxxxx(pthread_attr_t *attr,int val)形式的函数设置具体某个属性的值
2 同步属性
互斥量属性:pthread_mutexattr_t
int pthread_mutexattr_init(pthread_mutexattr_t *attr) int prthread_mutexattr_destroy(pthread_mutexattr_t *attr)
进程共享属性:
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_RECURSIVE:允许多次加锁,并提供错误检查(不占用解锁)
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_rwlockattr_getpshared(pthread_rwlockattr_t *attr,int *pshared) int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,int pshared)
条件变量属性:pthread_condattr_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)