#include <pthread.h> int pthread_attr_init(pthread_attr_t *attr) int pthread_attr_destroy(pthread_attr_t *attr)
線程屬性有四個:
1.detachstate 線程的分離狀態屬性 2.guardsize 線程棧末尾的警戒緩沖區大小(字節數) 3.stackaddr 線程棧的最低地址 4.stacksize 線程棧的最小長度 如果在創建線程時就知道不需要了解線程的終止狀態,可以修改pthread_attr_t結構中的detachstate屬性,讓線程一開始就處於分離狀態。可以使用pthread_attr_setdetachstate把線程屬性detachstate設置為以下兩個合法值之一:PTHREAD_CREATE_DETACHED,PTHREAD_CREATE_JOINABLE.#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; } View Code 遵循POSIX標准的系統,未必支持線程棧屬性,可以在編譯階段用_POSIX_THREAD_ATTR_STACKADDR和_POSIX_THREAD_ATTR_STACKSIZE符號來檢查系統是否支持線程棧屬性。 可以使用pthread_attr_getstack和pthread_attr_setstack對線程棧屬性進行管理#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為棧的最低內存地址。
也可以通過pthread_attr_getstacksize和pthread_attr_setstacksize讀取或設置線程屬性stacksize。#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)
二、互斥量屬性
對應非默認屬性,可以使用pthread_mutexattr_init初始化,pthread_mutexattr_destroy反初始化。#include <pthread.h> int pthread_mutexattr_init(pthread_mutexattr *attr) int pthread_mutexattr_destroy(pthread_mutexattr *attr)
互斥量屬性中值得注意的兩個屬性:進程共享屬性,類型屬性
當進程共享屬性設為PTHREAD_PROCESS_SHARED時,允許多個進程訪問共享數據,當進程共享屬性為PTHREAD_PROCESS_PRIVATE,則不允許。#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)
三、讀寫鎖屬性
使用pthread_rwlockattr_init初始化pthread_rwlockattr_t結構,用pthread_rwlockattr_destroy銷毀。#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://xxxxxx/Linuxjc/1152258.html TechArticle