日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程同步之互斥量加锁解锁 死锁

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程同步之互斥量加锁解锁 死锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

與互斥鎖相關API
??????互斥量(mutex)從本質上來說是一把鎖,在訪問共享資源前對互斥量進行加鎖,在訪問完成后釋放互斥量上的鎖。對互斥量進行加鎖后,任何其他試圖再次對互斥量加鎖的線程將會被阻塞直到當前線程釋放該互斥鎖。如果釋放互斥鎖時有多個線程阻塞,所有在該互斥鎖上的阻塞線程都會變成可運行狀態,第一個變為可運行狀態的線程可以對互斥量加鎖,其他線程將會看到互斥鎖依然被鎖住,只能回去等待它重新變為可用。在這種方式下,每次只有一個線程可以向前運行。

??????在設計時需要規定所有的線程必須遵守相同的數據訪問規則。只有這樣,互斥機制才能正常工作。操作系統并不會做數據訪問的串行化。如果允許其中的某個線程在沒有得到鎖的情況下也可以訪問共享資源,那么即使其它的線程在使用共享資源前都獲取了鎖,也還是會出現數據不一致的問題。

??????互斥變量用pthread_mutex_t數據類型表示。在使用互斥變量前必須對它進行初始化,可以把它置為常量PTHREAD_MUTEX_INITIALIZER(只對靜態分配的互斥量),也可以通過調用pthread_mutex_init函數進行初始化。如果動態地分配互斥量(例如通過調用malloc函數),那么在釋放內存前需要調用pthread_mutex_destroy。
1. 創建及銷毀互斥鎖

#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); int pthread_mutex_destroy(pthread_mutex_t *restrict mutex); // 返回:若成功返回0,否則返回錯誤編號

要用默認的屬性初始化互斥量,只需把attr設置為NULL。
2. 加鎖及解鎖

#include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *restrict mutex); int pthread_mutex_trylock(pthread_mutex_t mutex); int pthread_mutex_unlock(pthread_mutex_t *restrict mutex); // 返回:若成功返回0,否則返回錯誤編號

??????如果線程不希望被阻塞,它可以使用pthread_mutex_trylock嘗試對互斥量進行加鎖。如果調用pthread_mutex_trylock時互斥量處于未鎖住狀態,那么pthread_mutex_trylock將鎖住互斥量,不會出現阻塞并返回0,否則pthread_mutex_trylock就會失敗,不能鎖住互斥量,而返回EBUSY。

代碼示例

#include<stdio.h> #include<pthread.h> //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);int g_data=0; pthread_mutex_t mutex;//定義一個互斥量也就是鎖void*func1(void *arg) {int i;pthread_mutex_lock(&mutex);//給互斥量上鎖for(i=0;i<5;i++){printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));}pthread_mutex_unlock(&mutex);//給互斥量解鎖}void*func2(void *arg) {pthread_mutex_lock(&mutex);//給互斥量上鎖printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));pthread_mutex_unlock(&mutex);//給互斥量解鎖}int main() {int ret;int param=100;pthread_t t1;pthread_t t2;pthread_mutex_init(&mutex,NULL);//初始化互斥量ret=pthread_create(&t1,NULL,func1,(void*)&param);if(ret==0){printf("main:create t1 success\n");}ret=pthread_create(&t2,NULL,func2,(void*)&param);if(ret==0){printf("main:create t2 success\n");}printf("main : %ld \n",(unsigned long)pthread_self());pthread_join(t1,NULL);//用來等待進程t1退出pthread_join(t2,NULL);//用來等待進程t2退出pthread_mutex_destroy(&mutex);//銷毀這把鎖return 0; }

腳本可以這樣寫

CLC@Embed_Learn:~/xiancheng$ vi test.sh CLC@Embed_Learn:~/xiancheng$ chmod +x test.sh CLC@Embed_Learn:~/xiancheng$ ./test.sh //其中test.sh中寫入要運行的程序

實現進程t1滿足條件退出代碼

#include<stdio.h> #include<pthread.h> #include <stdlib.h> //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);int g_data=0;//pthread_mutex_t mutex;void*func1(void *arg) {printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));pthread_mutex_lock(&mutex);while(1){printf("t1 printf is %d\n",g_data++);sleep(1);if(g_data==3){pthread_mutex_unlock(&mutex);printf("t1 over==============================\n");//pthread_exit(NULL);exit(0);}}}void*func2(void *arg) {printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));while(1){printf("t2 printf is %d\n",g_data);pthread_mutex_lock(&mutex);g_data++; pthread_mutex_unlock(&mutex);sleep(1);}pthread_mutex_unlock(&mutex); }int main() {int ret;int param=100;pthread_t t1;pthread_t t2;pthread_mutex_init(&mutex,NULL);ret=pthread_create(&t1,NULL,func1,(void*)&param);if(ret==0){printf("main:create t1 success\n");}ret=pthread_create(&t2,NULL,func2,(void*)&param);if(ret==0){printf("main:create t2 success\n");}printf("main : %ld \n",(unsigned long)pthread_self());while(1){printf("main printf is %d\n",g_data);sleep(1);}pthread_join(t1,NULL);pthread_join(t2,NULL);pthread_mutex_destory(&mutex);return 0; }

什么情況下造成死鎖
??????死鎖 是指兩個或兩個以上的進程在執行過程中,由于競爭資源或彼此通信而造成的一種阻塞現象,無外力作用,他們都將無法再推進下去。(一般需要兩個mutex)
代碼示例

void*func1(void *arg) {int i;pthread_mutex_lock(&mutex);sleep(1);pthread_mutex_lock(&mutex2);for(i=0;i<5;i++){printf("t1 :%ld thread is created\n",(unsigned long)pthread_self());printf("t1:param is %d\n",*((int*)arg));}pthread_mutex_unlock(&mutex);}void*func2(void *arg) {pthread_mutex_lock(&mutex2);sleep(1);pthread_mutex_lock(&mutex);printf("t2 :%ld thread is created\n",(unsigned long)pthread_self());printf("t2:param is %d\n",*((int*)arg));pthread_mutex_unlock(&mutex);}

參考博文:https://www.cnblogs.com/xiehongfeng100/p/4620852.html

總結

以上是生活随笔為你收集整理的线程同步之互斥量加锁解锁 死锁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。