1. 執行緒屬性
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate); //獲取當前的detachstate執行緒屬性
int pthread_attr_setdetachstate(pthread_attr_t *restrict attr, int *detachstate);
//當detachstate為pthread_create_detached時,以分離狀態啟動執行緒;
//當detachstate為pthread_create_joinable時,正常啟動執行緒,應用程式可以獲取執行緒的終止狀態
#include "apue.h"
#include int makethread(void *(*fn)(void *), void *arg)
對執行緒棧的屬性更改
int pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrict stackaddr, size_t *restrict stacksize);
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
設定執行緒屬性stacksize
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控制著執行緒棧末尾之後用以避免棧溢位的擴充套件記憶體的大小。
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);
2. 同步屬性
i. 互斥量屬性
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_getshared(const pthread_mutex_attr_t *restrict attr, int *restrict pshared); //得到它的程序共享屬性
int pthread_mutexattr_setshared(pthread_mutexattr_t *attr, int pshared); //修改程序共享屬性
遞迴互斥量..
ii. 讀寫鎖屬性
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
讀寫鎖支援的唯一屬性是程序共享屬性。
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared);
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
iii. 條件變數屬性(程序共享屬性和時鐘屬性)
ix. 屏障屬性(程序共享屬性)
3. 執行緒特定資料
在分配執行緒特定資料之前,需要建立與該資料關聯的鍵。
int pthread_key_create(pthread_key_t *keyp, void (*destructor)(void *));
取消鍵與執行緒特定資料值之間的關聯關係
int pthread_key_delete(pthread_key_t key);
4. 執行緒和訊號
int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict oset);
int sigwait(const sigset_t *restrict set, int *restrict signop);//等待乙個或者多個訊號的出現
int pthread_kill(pthread_t thread, int signo);
#include "apue.h"
#include int quitflag;
sigset_t mask;
pthread_mutex_t lock = pthread_mutex_initializer;
pthread_cond_t waitloc = pthread_cond_initializer;
void *thr_fn(void *arg) }}
int main(void)
5. 執行緒和fork
多執行緒中清除鎖的狀態
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
#include "apue.h"
#include pthread_mutex_t lock1 = pthread_mutex_initializer;
pthread_mutex_t lock2 = pthread_mutex_initializer;
void prepare(void)
void parent(void)
void child(void)
void *thr_fn(void *arg)
int main(void)
/************************
thread started...
parent about to fork
prepare locks...
parent unlocking locks...
parent returned from fork
child unlocking locks...
child returned from fork
************************/
書上的結果是子程序先解鎖,然後再是父程序(???) APUE讀書筆記 第十二章 執行緒控制
初始化 銷毀屬性物件,每個屬性都有 從屬性物件中獲取屬性值 設定屬性值兩個個函式 實現執行緒分離的兩種方法 1 使用pthread detach函式 2 修改pthread attr t結構中的detachstate屬性 使用pthread attr setdetachstate函式 讓執行緒一開始...
第十二章 POSIX 執行緒(三)
用互斥量進行同步 另一種用在多執行緒程式中的同步訪問方法是使用互斥量。它允許程式設計師鎖住某個物件,使得每次只有乙個執行緒訪問它。為了控制對關鍵 的訪問,必須在進入這段 之前鎖住乙個互斥量,然後在完成操作之後解鎖它。include int pthread mutex init pthread mut...
第十二章 POSIX 執行緒(一)
在乙個程式中的多個執行線路就叫做執行緒 thread 更準確的定義是 執行緒是乙個程序內部的乙個控制序列。執行緒有一套完整的與其相關的函式庫呼叫,它們中的絕大多數函式名都是以 pthread 開頭,為了使用這些函式庫呼叫,我們必須定義巨集 peentrant,在程式中包含頭文 件 pthread.h...