UNIX高階程式設計 執行緒

2021-07-27 01:19:27 字數 3287 閱讀 6653

第11章執行緒

unix作業系統中多執行緒的存在允許同一時間處理多件事情

執行緒包含了表示程序內執行環境必需的資訊,其中包括程序中標識現場的執行緒id,一組暫存器值,棧,排程優先順序和策略等等

11.1 執行緒標識(pthread_id)

#include

int pthread_equal(pthread_t tid1,pthread_ttid2); //比較兩個執行緒id

pthread_t pthread_self(void);//獲得自身執行緒id

11.2 執行緒建立

int pthread_create(pthread_t *restricttidp,const pthread_attr_t* restrict attr,void *(*start_rtn)(void),void*restrict arg);

tidp:新建立執行緒的執行緒id

attr:定製執行緒的屬性

start_rtn:新建立的執行緒開始執行的入口

arg:start_rtn函式需要的引數,若引數個數》1,則需要將多個引數封裝在乙個結構中

11.3 執行緒終止

如果任一線程呼叫exit,_exit或者_exit,整個程序會終止

void pthread_exit(void *rval_ptr);  //終止單一執行緒,且不會造成整個程序的終止

int pthread_join(pthread_t thread,void**rval_ptr);//呼叫執行緒會一直阻塞,直到指定執行緒退出(包括呼叫pthread_exit,從啟動例程返回或者被同乙個程序的其他執行緒取消)

rval_ptr:標識指定執行緒退出的方式(終止狀態),退出碼,返回碼,取消碼

11.4執行緒取消

int pthread_cancel(pthread_t tid);//取消同乙個程序的其他執行緒

11.5 執行緒清理函式

void pthread_cleanup_push(void (*rtn)(void*),void *arg);//執行緒退出時的清理函式

rtn:清理函式

arg:函式引數

清理函式有效啟動的三種情況:

1) 呼叫pthread_exit

2) 響應取消請求

3) 用非零execute引數呼叫pthread_cleanup_pop

清理函式的呼叫順序和執行順序相反

int pthread_detach(pthread_t tid);//使得執行緒分離

11.6執行緒同步

執行緒同步的應用場景:當多個執行緒均共享同一片記憶體區域,且多個執行緒均能修改記憶體區域資料時,則會產生一致性問題,需要執行緒同步來解決(鎖)

互斥量(pthread_mutex_t):本質上就是一把鎖,當訪問共享資源時,首先會對互斥量加鎖,訪問完成後再解鎖

int pthread_mutex_init(pthread_mutex_t*restrict mutex,const pthread_mutexattr_t * restrict attr);//對互斥量進行初始化

attr:互斥量的定製屬性

int pthread_mutex_destroy(pthread_mutex_t*mutex);//對互斥量釋放

int pthread_mutex_lock(pthread_mutex_t*mutex); //互斥量加鎖(執行緒阻塞)

int pthread_mutex_trylock(pthread_mutex_t*mutex);//互斥量嘗試加鎖(執行緒不阻塞),若剛好加鎖互斥量,則不會阻塞並返回0,否則不能鎖住互斥量且會返回ebusy,能夠有利的解決死鎖問題

int pthread_mutex_unlock(pthread_mutex_t*mutex);//互斥量解鎖

鎖的使用要找到折中方案,鎖的粒度太大,會造成多個執行緒阻塞等待,而鎖的粒度太小則會導致開銷增大

讀寫鎖的三種狀態:讀模式加鎖狀態,寫模式加鎖狀態,不加鎖狀態,允許同一時間只有乙個執行緒占有寫模式的讀寫鎖,多個執行緒占有讀模式的讀寫鎖,當讀寫鎖處於寫加鎖狀態時,所有試圖對該鎖加鎖的執行緒均阻塞;當讀寫鎖處於讀模式狀態時,所有試圖以讀模式對它進行加鎖的執行緒均可以獲得訪問許可權,但如果執行緒希望以寫模式對此鎖進行加鎖,則必須阻塞等待全部的執行緒釋放讀鎖(非常適合對資料結構的讀多於寫的情況),也稱為共享-獨佔鎖

int pthread_rwlock_init(pthread_rwlock_t*restrict rwlock,const pthread_rwlockattr_t *restrict attr);  //初始化讀寫鎖

int pthread_rwlock_destroy(pthread_rwlock_t*rwlock); //釋放讀寫鎖

int pthread_rwlock_rdlock(pthread_rwlock_t*rwlock); //鎖定讀模式鎖

int pthread_rwlock_wrlock(pthread_rwlock_t*rwlock);//鎖定寫模式鎖

int pthread_rwlock_unlock(pthread_rwlock_t*rwlock);//解鎖

int pthread_rwlock_tryrdlock(pthread_rwlock_t*rwlock);//嘗試鎖定讀模式鎖

int pthread_rwlock_trywrlock(pthread_rwlock_t*rwlock);//嘗試鎖定寫模式鎖

條件變數

執行緒在改變條件之前必須首先鎖住互斥量(用互斥量保護條件變數),其他執行緒在獲得互斥量前不會察覺到這種改變,因為必須鎖定互斥量後才能計算條件。

int pthread_cond_init(pthread_cond_t*restrict cond,pthread_condattr_t *restrict attr);//初始化條件變數

int pthread _cond_destroy(pthread_cond_t*cond);//釋放條件變數

int pthread_cond_wait(pthread_cond_t*restrict cond,pthread_mutex_t *restrict mutex);//等待條件變數為真

int pthread_cond_timedwait(pthread_cond_t*restrict cond,pthread_mutex_t *restrict mutex,const structtimespec *restrict timeout);//單位時間限制

struct timespec//當前時間加上指定時間

int pthread_cond_signal(pthread_cond_t*cond);//喚醒等待條件的某乙個執行緒

int pthread_cond_broadcast(pthread_cond_t*cond);//喚醒等待條件的全部執行緒

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

第12章執行緒控制 12.1 執行緒的四大屬性 pthread attr t int pthread attr init pthread attr t attr 初始化執行緒屬性 int pthread attr destroy pthread attr t attr 釋放執行緒屬性 如果不關心執行...

unix環境高階程式設計 執行緒解析

1 通過為每種事件型別分配單獨的處理執行緒,可以簡化處理非同步事件的 2 多個程序必須使用作業系統提供的複雜機制才能實現記憶體和檔案描述符的共享,而多個執行緒自動地可以訪問相同的儲存位址空間和檔案描述符 3 互動的程式可以通過使用多執行緒來改善響應時間,多執行緒可以把程式中處理使用者輸入輸出的部分與...

Unix環境高階程式設計筆記 11 執行緒

1 執行緒概念 執行緒包含了表示程序內執行環境必須的資訊,其中包括程序中標識執行緒的執行緒id 一組暫存器值 棧 排程優先順序和策略 訊號遮蔽字 errno變數以及執行緒私有資料。程序的所有資訊對該程序的所有執行緒都是共享的,包括可執行的程式文字 程式的全域性記憶體和堆記憶體 棧以及檔案描述符。2 ...