日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
多線程與互斥鎖的實(shí)例分析

多線程與互斥鎖的實(shí)例分析,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比南岳網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式南岳網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋南岳地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

線程:線程是程序中的一個(gè)執(zhí)行流,每個(gè)線程都有自己的專有寄存器(棧指針、程序計(jì)數(shù)器等),但代碼區(qū)是共享的,即不同的線程可以執(zhí)行同樣的函數(shù)。

多線程:多線程是指程序中包含多個(gè)執(zhí)行流,即在一個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線程來執(zhí)行不同的任務(wù),也就是說允許單個(gè)程序創(chuàng)建多個(gè)并行執(zhí)行的線程來完成各自的任務(wù)。

線程創(chuàng)建

    函數(shù)原型:int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

    返回值:若是成功建立線程返回0,否則返回錯(cuò)誤的編號(hào)。

    形式參數(shù):pthread_t *restrict tidp要?jiǎng)?chuàng)建的線程的線程id指針;const pthread_attr_t  *restrict attr創(chuàng)建線程時(shí)的線程屬性;void  *(start_rtn)(void)返回值是void類型的指針函數(shù);void  *restrict arg start_rtn的形參。

    線程掛起:該函數(shù)的作用使得當(dāng)前線程掛起,等待另一個(gè)線程返回才繼續(xù)執(zhí)行。也就是說當(dāng)程序運(yùn)行到這個(gè)地方時(shí),程序會(huì)先停止,然后等線程id為thread的這個(gè)線程返回,然后程序才會(huì)斷續(xù)執(zhí)行。

    函數(shù)原型:int pthread_join(pthread_tthread, void **value_ptr);

    參數(shù)說明如下:thread等待退出線程的線程號(hào);value_ptr退出線程的返回值。

    返回值:若成功,則返回0;若失敗,則返回錯(cuò)誤號(hào)。

    線程退出

    函數(shù)原型:void pthread_exit(void *rval_ptr);

    獲取當(dāng)前線程id

    函數(shù)原型:pthread_t pthread_self(void);

互斥鎖

創(chuàng)建pthread_mutex_init;銷毀pthread_mutex_destroy;加鎖pthread_mutex_lock;解鎖pthread_mutex_unlock。

互斥量從本質(zhì)上說就是一把鎖, 提供對(duì)共享資源的保護(hù)訪問。

1. 初始化:

    在Linux下, 線程的互斥量數(shù)據(jù)類型是pthread_mutex_t. 在使用前, 要對(duì)它進(jìn)行初始化:

      對(duì)于靜態(tài)分配的互斥量, 可以把它設(shè)置為PTHREAD_MUTEX_INITIALIZER, 或者調(diào)用pthread_mutex_init.

      對(duì)于動(dòng)態(tài)分配的互斥量, 在申請(qǐng)內(nèi)存(malloc)之后, 通過pthread_mutex_init進(jìn)行初始化, 并且在釋放內(nèi)存(free)前需要調(diào)用pthread_mutex_destroy.

      原型:

      int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);

      int pthread_mutex_destroy(pthread_mutex_t *mutex);

      頭文件:

          返回值: 成功則返回0, 出錯(cuò)則返回錯(cuò)誤編號(hào).

      說明: 如果使用默認(rèn)的屬性初始化互斥量, 只需把a(bǔ)ttr設(shè)為NULL. 其他值在以后講解。

2. 互斥操作:

       對(duì)共享資源的訪問, 要對(duì)互斥量進(jìn)行加鎖, 如果互斥量已經(jīng)上了鎖, 調(diào)用線程會(huì)阻塞, 直到互斥量被解鎖. 在完成了對(duì)共享資源的訪問后, 要對(duì)互斥量進(jìn)行解鎖。

      首先說一下加鎖函數(shù):

      頭文件:

      原型:

      int pthread_mutex_lock(pthread_mutex_t *mutex);

      int pthread_mutex_trylock(pthread_mutex_t *mutex);

      返回值: 成功則返回0, 出錯(cuò)則返回錯(cuò)誤編號(hào).

       說明: 具體說一下trylock函數(shù), 這個(gè)函數(shù)是非阻塞調(diào)用模式, 也就是說, 如果互斥量沒被鎖住, trylock函數(shù)將把互斥量加鎖, 并獲得對(duì)共享資源的訪問權(quán)限; 如果互斥量被鎖住了, trylock函數(shù)將不會(huì)阻塞等待而直接返回EBUSY, 表示共享資源處于忙狀態(tài)。

      再說一下解所函數(shù):

      頭文件:

      原型: int pthread_mutex_unlock(pthread_mutex_t *mutex);

      返回值: 成功則返回0, 出錯(cuò)則返回錯(cuò)誤編號(hào).

      3. 死鎖:

    死鎖主要發(fā)生在有多個(gè)依賴鎖存在時(shí), 會(huì)在一個(gè)線程試圖以與另一個(gè)線程相反順序鎖住互斥量時(shí)發(fā)生. 如何避免死鎖是使用互斥量應(yīng)該格外注意的東西。

      總體來講, 有幾個(gè)不成文的基本原則:

      對(duì)共享資源操作前一定要獲得鎖。

      完成操作以后一定要釋放鎖。

      盡量短時(shí)間地占用鎖。

      如果有多鎖, 如獲得順序是ABC連環(huán)扣, 釋放順序也應(yīng)該是ABC。

      線程錯(cuò)誤返回時(shí)應(yīng)該釋放它所獲得的鎖。

條件鎖

創(chuàng)建pthread_cond_init;銷毀pthread_cond_destroy;觸發(fā)pthread_cond_signal;廣播pthread_cond_broadcast;等待pthread_cond_wait。以后補(bǔ)充

下面以一個(gè)demo程序了解一下,

#include
#include
#include
#include
#include
void * print_a(void *);
void * print_b(void *);
static pthread_mutex_t testlock;
int main(void)
{
  pthread_t t0;
  pthread_t t1;
  pthread_mutex_init(&testlock,NULL);
  
  //create thread a
  if(pthread_create(&t0,NULL,print_a,NULL)){
    puts("fail to create pthread a");
    exit(1);
  }
  //create thread b
  if(pthread_create(&t1,NULL,print_b,NULL)){
    puts("fail to create pthread b");
    exit(1);
  }
  void *result;
  if(pthread_join(t0,&result)==-1){
    puts("fail to reconnect to");
    exit(1);
  }
  if(pthread_join(t1,&result)==-1){
    puts("fail to reconnect t1");
    exit(1);
  }
  pthread_mutex_destroy(&testlock);
  return 0;
}
void * print_a(void *a){
  int i=0;
  pthread_mutex_lock(&testlock);
  for(i=0;i<10;i++){
    sleep(1);
    puts("aa");
    if(i==4){
      pthread_mutex_unlock(&testlock);
      sleep(1);
      pthread_mutex_lock(&testlock);
    }
  }
  pthread_mutex_unlock(&testlock);
  return NULL;
}
void * print_b(void *b){
  int i=0;
  pthread_mutex_lock(&testlock);
  for(i=0;i<20;i++){
    sleep(1);
    puts("bb");
    if(i==6){
      pthread_mutex_unlock(&testlock);
      sleep(1);
      pthread_mutex_lock(&testlock);
    }
  }
  pthread_mutex_unlock(&testlock);
  return NULL;
}

運(yùn)行結(jié)果如下:

aa
aa
aa
aa
aa
bb
bb
bb
bb
bb
bb
bb
aa
aa
aa
aa
aa
bb
bb
bb
bb
bb
bb
bb
bb
bb
bb
bb
bb
bb

我們創(chuàng)建了兩個(gè)進(jìn)程t0和t1,以及互斥鎖testlock。當(dāng)兩個(gè)線程同時(shí)運(yùn)行的時(shí)候,線程t0上鎖以后,線程t1等待線程t0釋放鎖,線程t1加鎖以后線程t0,等待,所以出現(xiàn)了如上的運(yùn)行結(jié)果。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。


本文標(biāo)題:多線程與互斥鎖的實(shí)例分析
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/ggdggj.html