标签:
#include <pthread.h> int pthread_attr_init(pthread_attr_t *attr) int pthread_attr_destroy(pthread_attr_t *attr)
线程属性有四个:
#include <pthread.h> int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate); int pthread_attr_setdetachstate(const pthread_attr_t *attr, int *detachstate);
例子:
#include "apue.h" #include <pthread.h> int makethread(void *(*fn)(void *), void *arg) { int err; pthread_t tid; pthread_attr_t attr; err = pthread_attr_init(&attr); if (err != 0) { return err; } err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (err == 0) { err = pthread_create(&tid, &attr, fn, arg); } pthread_attr_destroy(&attr); return err; }
#include <pthread.h> int pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrick stackaddr, size_t *restrict stacksize) int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
如果线程栈的虚地址空间用完了,那可以使用malloc或者mmap来为可替代的栈分配空间,stackaddr为栈的最低内存地址。
#include <pthread.h> int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize) int pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize)
线程属性guardsize控制着线程栈末尾之后用以避免栈溢出的扩展内存的大小。
#include <phtread.h> int pthread_attr_getguardsize(const pthread_attr_t *restrict attr, size_t *restrict guardsize) int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
二、互斥量属性
#include <pthread.h> int pthread_mutexattr_init(pthread_mutexattr *attr) int pthread_mutexattr_destroy(pthread_mutexattr *attr)
互斥量属性中值得注意的两个属性:进程共享属性,类型属性
#include <pthread.h> int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared) int pthread_mutexattr_setpshared(const pthread_muteattr_t *attr, int pshared)
类型属性控制住互斥量的锁定特性,值得注意的是其中的PTHREAD_MUTEX_RECURSIVE类型,此类型允许同一线程在互斥量解锁之前对该互斥量进行多次加锁。递归互斥量维护锁的基数,在解锁次数和加锁次数不相同的情况下,不解锁。
#include <pthread.h> int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type) int pthread_mutexattr_settype(pthread_mutexattr *attr, int type)
三、读写锁属性
#include <pthread.h> int pthread_rwlockattr_init(pthread_rwlockattr_t *attr) int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
读写锁唯一属性是进程共享属性,与互斥量的进程共享属性相同。
#include <pthread.h> int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared) int pthread_rwlockattr_setpshared(const pthread_rwlockattr_t *attr, int * pshared)
四、条件变量属性
#include <pthread.h> int pthread_condattr_init(pthread_condattr_t *attr) int pthread_condattr_destroy(pthread_condattr_t *attr)
条件变量支持进程共享属性和时钟属性,其中进程共享属性与互斥量的进程共享属性相同。
#include <pthread.h> int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared) int pthread_condattr_setpshared(const pthread_condattr_t *attr, int pshared)
时钟属性控制pthread_cond_timedwait函数的超时参数tsptr采用的是哪个时钟。
#include <pthread.h> int pthread_condattr_getclock(const pthread_condattr_t *restrict attr, clockid_t *restrict clock_id) int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
标签:
原文地址:http://www.cnblogs.com/shenlinken/p/5801472.html