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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux编程练习 --多线程3--mutex

發(fā)布時(shí)間:2023/12/10 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux编程练习 --多线程3--mutex 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

互斥指互相排斥的鎖,是一種信號量,常用來防止兩個(gè)進(jìn)程或線程在同一時(shí)刻訪問相同的共享資源

????????

1.數(shù)據(jù)類型:

?Linux,?線程的互斥量數(shù)據(jù)類型是pthread_mutex_t,我們定義一個(gè)互斥數(shù)據(jù)可以這樣:

????????????? pthread_mutex_t mutex;

?

2.函數(shù)說明:
頭文件:?????pthread.h

(1).互斥鎖初始化:

?

函數(shù)原型:?int pthread_mutex_init (pthread_mutex_t* mutex,

?????????????????????????????????const pthread_mutexattr_t*???mutexattr);
函數(shù)傳入值:? mutex:互斥鎖。

????????????????mutexattr:PTHREAD_MUTEX_INITIALIZER:創(chuàng)建快速互斥鎖。

????????????????PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:創(chuàng)建遞歸互斥鎖。

???????????????????PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:創(chuàng)建檢錯(cuò)互斥鎖。

函數(shù)返回值:?成功:0

???????????????????出錯(cuò):-1?
(2).互斥操作函數(shù)

int pthread_mutex_lock(pthread_mutex_t* mutex);//上鎖

int pthread_mutex_trylock (pthread_mutex_t* mutex);//只有在互斥被鎖住的情況下才阻塞

int pthread_mutex_unlock (pthread_mutex_t* mutex);//解鎖

int pthread_mutex_destroy (pthread_mutex_t* mutex);//清除互斥鎖

函數(shù)傳入值:??????????? mutex:互斥鎖。

函數(shù)返回值:???????????成功:0

????????????????????????????? 出錯(cuò):-1

3.使用形式:

struct mutex mutex;

mutex_init(&mutex); /*定義*/

...
mutex_lock(&mutex); /*獲取互斥鎖*/

... /*臨界資源*/

mutex_unlock(&mutex); /*釋放互斥鎖*/

?

?

最后進(jìn)行一個(gè)練習(xí):我們創(chuàng)建兩個(gè)線程,分別訪問全局變量gnum,并且修改它,打印出來

[cpp]?view plaincopy
  • /*mutex.c*/??
  • #include?<stdlib.h>????
  • #include?<stdio.h>????
  • #include?<pthread.h>????
  • #include?<errno.h>?????
  • /*全局變量*/??
  • int?gnum?=?0;??
  • /*互斥量?*/??
  • pthread_mutex_t?mutex;??
  • /*聲明線程運(yùn)行服務(wù)程序*/??
  • static?void?pthread_func_1?(void);?????
  • static?void?pthread_func_2?(void);?????
  • ????
  • int?main?(void)?????
  • {?????
  • ?/*線程的標(biāo)識符*/??
  • ??pthread_t?pt_1?=?0;?????
  • ??pthread_t?pt_2?=?0;?????
  • ??int?ret?=?0;?????
  • ??/*互斥初始化*/??
  • ??pthread_mutex_init?(&mutex,?NULL);???
  • ??/*分別創(chuàng)建線程1、2*/??
  • ??ret?=?pthread_create?(&pt_1,??????????//線程標(biāo)識符指針??
  • ?????????????????????????NULL,??????????//默認(rèn)屬性??
  • ????????????????????????(void?*)pthread_func_1,//運(yùn)行函數(shù)??
  • ????????????????????????NULL);??????????//無參數(shù)??
  • ??if?(ret?!=?0)?????
  • ??{?????
  • ?????perror?("pthread_1_create");?????
  • ??}?????
  • ????
  • ??ret?=?pthread_create?(&pt_2,??????????//線程標(biāo)識符指針??
  • ????????????????????????NULL,???????????//默認(rèn)屬性????
  • ????????????????????????(void?*)pthread_func_2,?//運(yùn)行函數(shù)??
  • ????????????????????????NULL);??????????//無參數(shù)??
  • ??if?(ret?!=?0)?????
  • ??{?????
  • ?????perror?("pthread_2_create");?????
  • ??}?????
  • ??/*等待線程1、2的結(jié)束*/??
  • ??pthread_join?(pt_1,?NULL);?????
  • ??pthread_join?(pt_2,?NULL);?????
  • ????
  • ??printf?("main?programme?exit!/n");????
  • ??return?0;?????
  • }?????
  • /*線程1的服務(wù)程序*/??
  • static?void?pthread_func_1?(void)?????
  • {?????
  • ??int?i?=?0;?????
  • ???????
  • ??for?(;;)?????
  • ??{?????
  • ????printf?("This?is?pthread1!/n");??????
  • ????pthread_mutex_lock(&mutex);?/*獲取互斥鎖*/??
  • ????/*注意,這里以防線程的搶占,以造成一個(gè)線程在另一個(gè)線程sleep時(shí)多次訪問互斥資源,所以sleep要在得到互斥鎖后調(diào)用*/??
  • ????sleep?(1);????
  • ????/*臨界資源*/??
  • ????gnum++;??
  • ????printf?("Thread1?add?one?to?num:%d/n",gnum);??
  • ????pthread_mutex_unlock(&mutex);?/*釋放互斥鎖*/??
  • ??
  • ???????
  • ??}?????
  • }?????
  • /*線程2的服務(wù)程序*/???
  • static?void?pthread_func_2?(void)?????
  • {?????
  • ??int?i?=?0;?????
  • ????
  • ??for?(;;)?????
  • ??{?????
  • ????printf?("This?is?pthread2!/n");???
  • ????pthread_mutex_lock(&mutex);?/*獲取互斥鎖*/??
  • ????/*注意,這里以防線程的搶占,以造成一個(gè)線程在另一個(gè)線程sleep時(shí)多次訪問互斥資源,所以sleep要在得到互斥鎖后調(diào)用*/??
  • ????sleep?(1);??
  • ????/*臨界資源*/??
  • ????gnum++;??
  • ????printf?("Thread2?add?one?to?num:%d/n",gnum);??
  • ????pthread_mutex_unlock(&mutex);?/*釋放互斥鎖*/??
  • ????
  • ??}?????
  • ????
  • ??pthread_exit?(0);?????
  • }????
  • 然后編譯,運(yùn)行,看到是線程1,2分別和平地訪問共享資源

    總結(jié)

    以上是生活随笔為你收集整理的Linux编程练习 --多线程3--mutex的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。