UNIX高階程式設計 執行緒控制

2021-07-27 02:13:31 字數 4170 閱讀 4611

第12章執行緒控制

12.1 執行緒的四大屬性(pthread_attr_t)

int pthread_attr_init(pthread_attr_t *attr);// 初始化執行緒屬性

int pthread_attr_destroy(pthread_attr_t*attr);//釋放執行緒屬性

如果不關心執行緒退出時的終止狀態,只需要pthread_detach()將執行緒設定為分離狀態,這樣在該執行緒退出時會自動**占有的資源

int pthread_attr_setdetachstate(pthread_attr_t*attr,int detachstate);

detachstate:pthread_create_detached:分離狀態啟動執行緒

pthread_create_joinable正常狀態啟動執行緒

int pthread_attr_getdetachstate(constpthread_attr_t *restrict attr,int *detachstate);//返回detachstate執行緒屬性

執行緒棧屬性查詢和修改

int pthread_attr_getstack(constpthread_attr_t *restrict attr,void **restrict stackaddr,size_t *restrictstacksize); //查詢執行緒棧屬性(包括執行緒棧的位址和大小)

int pthread_attr_setstack(const pthread_attr_t *attr,void *stackaddr,size_t*stacksize);//設定執行緒棧屬性

執行緒棧的管理:

執行緒棧大小:當執行緒數量過多,致使執行緒棧的累計大小超過可用的虛擬位址空間時,則需要減少預設的執行緒棧大小;當執行緒呼叫的函式分配了大量的自動變數或者呼叫的函式涉及很深的棧幀時,則需要增加預設的執行緒棧大小。

int pthread_attr_getstacksize(constpthread_attr_t *restrict attr,size_t *restrict stacksize);

int pthread_attr_setstacksize(pthread_attr_t*attr,size_t stacksize);

如果用完了執行緒棧的虛擬位址空間,則需要使用malloc或者mmap為其他棧分配空間,pthread_attr_setstack函式改變新建執行緒的棧位置。執行緒棧所佔記憶體的可定址的最低位址可由stackaddr引數指定,與處理器結構相應的邊界對齊。

int pthread_attr_getguardsize(constpthread_attr_t *restrict attr,size_t *restrict guardsize);

int pthread_attr_setguardsize(constpthread_attr_t * attr,size_t  guardsize);

警戒緩衝區大小(guardsize):避免執行緒棧一處的擴充套件記憶體大小,預設是pagesize個位元組

併發度:控制著使用者級執行緒對映到核心級執行緒的數量

int pthread_getconcurrency(void);//返回希望的並行度

int pthread_setconcurrenct(int level);//設定希望的並行度,只是乙個提示並不代表系統會採納

12.2同步屬性

1)互斥量屬性

int pthread_mutexattr_init(pthread_mutexattr_t*attr);//初始化互斥量屬性

int pthread_mutexattr_destroy(pthread_mutexattr_t*attr);//釋放互斥量屬性

程序共享屬性:pthread_process_private 允許多執行緒訪問同乙個同步物件

pthread_process_shared 允許多程序訪問同乙個同步物件

型別屬性:pthread_mutex_normal標準互斥量,不做錯誤檢查或者死鎖檢測

pthread_mutex_errorcheck提供錯誤檢查

pthread_mutex_recursive允許同乙個執行緒在互斥量解鎖之前對該互斥量進行多次加鎖,用遞迴互斥量維護鎖的次數,當加鎖次數和解鎖次數不相同時,不會釋放鎖。

pthread_mutex_default:請求預設語義

int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrictattr,int *restrict type);  //返回互斥量的型別屬性

int pthread_mutexattr_settype(constpthread_mutexattr_t attr,int type);//設定互斥量的型別屬性

讀寫鎖屬性:

int pthread_rwlockattr_init(pthread_rwlockattr_t*attr);

int pthread_rwlockattr_destroy(pthread_rwlockattr_t*attr);

程序共享屬性與互斥量的程序共享屬性相同

條件變數屬性支援程序共享屬性

int pthread_condattr_init(pthread_condattr_t*attr);

int pthread_condattr_destroy(pthread_condattr_t*attr);

12.3 重入

乙個函式對多個執行緒是可重入的,則說該函式是執行緒安全的

執行緒安全管理file物件的方法:

int ftrylockfile(file *fp);

void flockfile(file *fp);

void funlockfile(file *fp);

12.4 執行緒私有資料

執行緒私有資料是儲存和查詢與某個執行緒相關的資料,分配執行緒私有資料之前,需要建立於該資料相關的鍵

int pthread_key_create(pthread_key_t*keyp,void (*destructor)(void *));

keyp:儲存鍵

destructor:與該建相關的析構函式,只有執行緒執行返回或者pthread_exit正常退出時才會呼叫析構函式

int pthread_key_delete(pthread_key_t *key);//取消鍵與執行緒私有資料值之間的關聯關係

int pthread_once(pthread_once_t*initflag,void (*initfn)(void));

void *pthread_getspecific(pthread_key_t key);

int pthread_setspecific(pthread_key_tkey,const void *value);//將key與執行緒私有資料相關聯

int pthread_once(pthread_once_t*initflag,void (*initfn)(void));//初始化例程呼叫一次

12.5取消選項

int pthread_setcancelstate(int state,int*oldstate);

state:pthread_cancel_enable(可取消狀態)

pthread_cancel_disable(不可取消狀態)

void pthread_testcancel(void);//新增取消點,當某個取消請求處於未決狀態,而且取消並沒有置為無效,則執行緒會被取消

int pthread_setcanceltype(int type,int*oldtype);//設定可取消型別

pthread_cancel_deferred(延遲取消,當呼叫pthread_cancel之後,在取消點之前,執行緒不會執行取消操作)

pthread_cancel_asynchronous(非同步取消,執行緒可以任意時間取消)

12.9執行緒與fork

執行緒呼叫fork,為子程序建立了整個程序的位址空間的副本,fork返回後,子程序會清理鎖狀態,子程序內部只存在乙個父程序呼叫fork的執行緒的副本

int pthread_atfork(void (*prepare)(void),void(*parent)(void),void (*child)(void)); //fork處理程式

prepare:在父程序呼叫fork()建立子程序之前,獲取父程序的全部鎖(註冊順序相反)

parent:在建立子程序之後,fork返回之前在父程序環境中進行呼叫,解鎖(註冊順序相同)

child在fork返回之前在子程序環境中進行呼叫,解鎖(註冊順序相同)

《unix高階環境程式設計》執行緒控制 同步屬性

互斥量屬性可以用 pthread mutexattr t 資料結構來進行操作,屬性的初始化操作如下 cpp view plain copy 同步屬性 互斥量屬性 函式功能 初始化互斥量屬性 返回值 若成功則返回0,否則返回錯誤編碼 函式原型 include intpthread mutexattr ...

unix執行緒控制

include int pthread pthread t restrict tipd,const pthread attr t restrict attr,void start rtn void void restrict arg 返回值 若成功返回0 否則,返回錯誤編號fork 子執行緒複製父執...

Unix環境高階程式設計 程序控制 二

一 函式wait 和waitpid 今天我們繼續通過昨天那個死爹死兒子的故事來講 便於記憶 現在看看 wait 和waitpid 函式。include pid t wait int statloc pid t waitpid pid t pid int statloc int options 若成功...