12 執行緒控制

2021-07-11 09:31:11 字數 4340 閱讀 3128

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

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

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); 

int pthread_attr_getdetachstate(pthread_attr_t *attr, int

*detachstate);

detatchstate取值為:

(1)pthread_create_detached 分離狀態啟動,

(2)pthread_create_joinable 正常啟動,應用程式可以獲取執行緒的終止狀態。

(1)互斥量屬性

有程序共享屬性和型別屬性兩種,程序共享屬性是可選的,互斥量屬性資料型別為pthread_mutexattr_t。在程序中,多個執行緒可以訪問同乙個同步物件,預設情況程序共享互斥量屬性為:pthread_process_private。

int pthread_mutexattr_init(pthread_mutexattr_t *attr);  //初始化

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); //**

int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared); //查詢程序共享屬性

int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,int pshared); //設定程序共享屬性

int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,int *restrict type); //查詢型別屬性

int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); //設定型別屬性

(2)讀寫鎖屬性:

與互斥量類似,但是只支援程序共享唯一屬性,操作函式原型如下:

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);

(3)條件變數屬性:

也是只支援程序共享屬性,操作函式原型如下:

int pthread_condattr_destroy(pthread_condattr_t *attr);

int pthread_condattr_init(pthread_condattr_t *attr);

int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr,int

*restrict pshared);

int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared);

執行緒模型促進了執行緒中資料和屬性的共享,為什麼還要提出合適的用於阻止共享的介面呢?

第一. 有時候需要維護基於每個執行緒的資料線程id不能保證是小而連續的整數,即使能保證,也要防止執行緒間資料的混淆。

第二. 它提供了讓基於程序的介面適應多執行緒環境的機制。

分配執行緒私有資料過程:首先呼叫pthread_key_create建立與該資料關聯的鍵,用於獲取對執行緒私有資料的訪問權,這個鍵可以被程序中所有執行緒訪問,但是每個執行緒把這個鍵與不同的執行緒私有資料位址進行關聯然後通過呼叫pthread_setspecific函式把鍵和執行緒私有資料關聯起來,可以通過pthread_getspecific函式獲取執行緒私有資料的位址。

pthread_key_create函式可以選擇為該鍵關聯的析構函式,呼叫pthread_key_delete函式來取消與執行緒私有資料值之間的關聯關係。通過呼叫pthread_once函式確保分配的鍵並不會由於在初始化階段的競爭而發生變動。

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

int pthread_key_delete(pthread_key_t key);

int pthread_once(pthread_once_t *once_control,void (*init_routine)(void)); //避免競爭條件

pthread_once_t once_control = pthread_once_init;

void *pthread_getspecific(pthread_key_t key); //返回執行緒私有資料值,沒有返回null

int pthread_setspecific(pthread_key_t key, const

void *value); //設定執行緒私有資料

#include 

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);

int sigwait(const sigset_t *set, int *sig);

int pthread_kill(pthread_t thread, int sig);

同步訊號案例

//同步訊號

#include "apue.h"

#include

int quitflag; /* set nonzero by thread */

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)

子程序從父程序那裡繼承了所有互斥量、讀寫鎖和條件變數的狀態。要清除鎖狀態,可以通過呼叫pthread_atfork函式建立fork處理程式

#include

int pthread_atfork(void (*prepare)(void), void (*

parent)(void), void (*child)(void));

returns: 0

if ok, error number on failure

示例

#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)

執行結果:

$ ./a.out

thread started...

parent about to fork...

preparing locks...

child unlocking locks...

child returned from fork

parent unlocking locks...

parent returned from fork

12 執行緒控制

1.執行緒屬性 2.同步屬性 2.1 互斥量屬性 值得注意的幾個 pthread mutexattr setrobust pthread mutex consistent pthread mutexattr gettype 2.2 讀寫鎖的屬性 2.3 條件變數屬性 pthread condattr...

12 流程控制

作用 主要用於判讀語句中,用來判斷 else語句 elif語句 巢狀的if.elif.else構造 if expression1 statement if expression2 statement else statement else statement 使用邏輯運算子and or not if...

執行緒控制 join執行緒

在我們做專案的時候時常會有這樣的一種需求 我們需要執行兩個方法,乙個方法要等另乙個方法執行完才能執行,這樣的狀況放到多執行緒中要怎麼實現呢?今天就來看看多執行緒中的join方法。我們的均方法通常是把乙個大問題分成許多小問題,每個小問題分配乙個執行緒,當所有的小問題都得到處理後,在呼叫主線程來進一步操...