執行緒,互斥鎖,條件

2021-09-26 07:22:55 字數 3130 閱讀 7359

執行緒(英語:thread)是作業系統能夠進行運算排程的最小單位。它被包含在程序之中,是程序中的實際運作單位。一條執行緒指的是程序中乙個單一順序的控制流,乙個程序中可以併發多個執行緒,每條執行緒並行執行不同的任務。在unix system v及sunos中也被稱為輕量程序(lightweight processes),但輕量程序更多指核心執行緒(kernel thread),而把使用者執行緒(user thread)稱為執行緒。簡單來說,我們可以在**中執行多個while死迴圈即可以同一時間做多件事情。

pthread_create函式

函式簡介

pthread_create是unix環境建立執行緒函式

標頭檔案#include

函式宣告

int pthread_create(pthread_t *restrict tidp,const pthread_attr_t restrict_attr,void(start_rtn)(void),void *restrict arg);

返回值若成功則返回0,否則返回出錯編號

引數第乙個引數為指向執行緒識別符號的指標。

第二個引數用來設定執行緒屬性。

第三個引數是執行緒執行函式的位址。

最後乙個引數是執行函式的引數。

注意在編譯時注意加上-lpthread引數,以呼叫靜態鏈結庫。因為pthread並非linux系統的預設庫。

pthread_join函式

標頭檔案: pthread.h

函式簡介

函式pthread_join用來等待乙個執行緒的結束。

函式原型為:

extern int pthread_join __p (pthread_t __th, void **__thread_return);

引數:第乙個引數為被等待的執行緒識別符號

第二個引數為乙個使用者定義的指標,它可以用來儲存被等待執行緒的返回值。

注意這個函式是乙個執行緒阻塞的函式,呼叫它的函式將一直等待到被等待的執行緒結束為止,當函式返回時,被等待執行緒的資源被收回。如果執行成功,將返回0,如果失敗則返回乙個錯誤號。

pthread_exit

標頭檔案: pthread.h

函式原型: void pthread_exit (void* retval);

函式傳入值:retval:pthread_exit()呼叫者執行緒的返回值,可又其他函式如pthread_join來檢索獲取。

phread_join

標頭檔案: pthread.h

函式原型: int pthread_join (pthread_t thread, void** thread_return);

函式傳入值:thread:等待執行緒的識別符號。

thread_return:使用者定義的指標,用來儲存被等待執行緒的返回值(不為null值);

函式返回值:成功: 0

失敗:-1

#include #include void *func1(void *arg)

int main()

互斥鎖

int pthread_mutex_init (pthread_mutex_t* mutex,const pthread_mutexattr_t* mutexattr);

功能:初始化互斥量

//亦可 pthread_mutex_t mutex = pthread_mutex_initializer;

​int pthread_mutex_lock (pthread_mutex_t* mutex);

功能:加鎖

​int pthread_mutex_unlock (pthread_mutex_t* mutex);

功能:解鎖

int pthread_mutex_destroy (pthread_mutex_t* mutex);

功能:銷毀互斥量

互斥量被初始化為非鎖定狀態;

執行緒1呼叫pthread_mutex_lock函式,立即返回,互斥量呈鎖定狀態;

執行緒2呼叫pthread_mutex_lock函式,阻塞等待;

執行緒1呼叫pthread_mutex_unlock函式,互斥量呈非鎖定狀態;

執行緒2被喚醒,從pthread_mutex_lock函式中返回,互斥量呈鎖定狀態;

#include #include pthread_mutex_t mutex;

void *func1(void *arg)

void *func2(void *arg)

int main()

執行緒條件

條件變數是利用執行緒間共享得全域性變數進行同步的一種機制,主要包括兩個動作:乙個執行緒等待「條件變數的條件成立」而掛起;另乙個執行緒使「條件成立」給出條件成立訊號。為了防止競爭,條件變數得使用總是和乙個互斥鎖結合在一起。

1、建立和登出

條件變數和互斥鎖一樣,有兩種建立方式,靜態方式使用pthread_cond_initializer,動態方式使用pthread_coud_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)函式,cond_attr設定為null即可。登出需要使用int pthread_cond_destroy(pthread_cond_t *cond);

2、等待和激發

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)

#include #include int a = 0;

pthread_mutex_t mutex;

pthread_cond_t cond;

void *func1(void *arg)

printf("1\n");

pthread_exit((void *)&i);

}void *func2(void *arg)

pthread_mutex_unlock(&mutex);

sleep(1);

}pthread_exit((void *)&i);

}int main()

執行緒同步 互斥鎖 條件變數

在 執行緒同步 互斥鎖 一文中,我們分析了只用互斥鎖同步執行緒的弊端 cpu的效率和時效性不可兼得。下面,我們通過使用條件變數,在保證cpu效率的前提下,提高程式的時效性。只用互斥鎖同步執行緒,其cpu佔用率之所以高,是因為執行緒需要輪詢,即需要不停的檢查條件是否滿足。我們使用條件變數,當條件不滿足...

同步執行緒 條件變數與互斥鎖

一。互斥量和條件變數簡介 互斥量 mutex 從本質上說是一把鎖,在訪問共享資源前對互斥量進行加鎖,在訪問完成後釋放互斥量上的鎖。對互斥量進行加鎖以後,任何其他試圖再次對互斥鎖加鎖的執行緒將會阻塞直到當前執行緒釋放該互斥鎖。如果釋放互斥鎖時有多個執行緒阻塞,所有在該互斥鎖上的阻塞執行緒都會變成可執行...

多執行緒程式設計 互斥鎖 條件變數

一,互斥鎖 在多工作業系統中,有很多任務同時執行,這些任務可能會用到同乙個資源,如果沒有一種機制來控制這些任務共享同乙個資源,那這些任務可能無法正常使用自己想用的資源。互斥鎖 是多工作業系統中一種簡單的加鎖方法,來控制各任務對共享資源的訪問。互斥鎖的狀態 上鎖 lock 和解鎖 unlock 互斥鎖...