成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Linux互斥鎖之線程互斥鎖

系統(tǒng) Linux
在編程中,引入了對(duì)象互斥鎖的概念,來保證共享數(shù)據(jù)操作的完整性。每個(gè)對(duì)象都對(duì)應(yīng)于一個(gè)可稱為" 互斥鎖" 的標(biāo)記,這個(gè)標(biāo)記用來保證在任一時(shí)刻,只能有一個(gè)線程訪問該對(duì)象。

 [[339322]]

在編程中,引入了對(duì)象互斥鎖的概念,來保證共享數(shù)據(jù)操作的完整性。每個(gè)對(duì)象都對(duì)應(yīng)于一個(gè)可稱為" 互斥鎖" 的標(biāo)記,這個(gè)標(biāo)記用來保證在任一時(shí)刻,只能有一個(gè)線程訪問該對(duì)象。

Linux實(shí)現(xiàn)的互斥鎖機(jī)制包括POSIX互斥鎖和內(nèi)核互斥鎖,本文主要講POSIX互斥鎖,即線程間互斥鎖。

“ 信號(hào)量用在多線程多任務(wù)同步的,一個(gè)線程完成了某一個(gè)動(dòng)作就通過信號(hào)量告訴別的線程,別的線程再進(jìn)行某些動(dòng)作(大家都在sem_wait的時(shí)候,就阻塞在 那里)。而互斥鎖是用在多線程多任務(wù)互斥的,一個(gè)線程占用了某一個(gè)資源,那么別的線程就無法訪問,直到這個(gè)線程unlock,其他的線程才開始可以利用這 個(gè)資源。比如對(duì)全局變量的訪問,有時(shí)要加鎖,操作完了,在解鎖。有的時(shí)候鎖和信號(hào)量會(huì)同時(shí)使用的”

也就是說,信號(hào)量不一定是鎖定某一個(gè)資源,而是 流程上的概念,比如:有A,B兩個(gè)線程,B線程要等A線程完成某一任務(wù)以后再進(jìn)行自己下面的步驟,這個(gè)任務(wù)并不一定是鎖定某一資源,還可以是進(jìn)行一些計(jì)算 或者數(shù)據(jù)處理之類。而線程互斥量則是“鎖住某一資源”的概念,在鎖定期間內(nèi),其他線程無法對(duì)被保護(hù)的數(shù)據(jù)進(jìn)行操作。在有些情況下兩者可以互換。

兩者之間的區(qū)別:

作用域

信號(hào)量 : 進(jìn)程間或線程間(linux僅線程間)

互斥鎖 : 線程間

上鎖時(shí)

信號(hào)量 : 只要信號(hào)量的value大于0,其他線程就可以sem_wait成功,成功后信號(hào)量的value減一。若value值不大于0,則sem_wait阻塞,直到sem_post釋放后value值加一。一句話,信號(hào)量的value>=0 。

互斥鎖 : 只要被鎖住,其他任何線程都不可以訪問被保護(hù)的資源。如果沒有鎖,獲得資源成功,否則進(jìn)行阻塞等待資源可用。一句話,線程互斥鎖的vlaue可以為負(fù)數(shù) 。

多線程

線程是計(jì)算機(jī)中獨(dú)立運(yùn)行的最小單位,運(yùn)行時(shí)占用很少的系統(tǒng)資源。與多進(jìn)程相比,多進(jìn)程具有多進(jìn)程不具備的一些優(yōu)點(diǎn),其最重要的是:對(duì)于多線程來說,其能夠比多進(jìn)程更加節(jié)省資源。

 

線程創(chuàng)建

在Linux中,新建的線程并不是在原先的進(jìn)程中,而是系統(tǒng)通過一個(gè)系統(tǒng)調(diào)用clone()。該系統(tǒng)copy了一個(gè)和原先進(jìn)程完全一樣的進(jìn)程,并在這個(gè)進(jìn)程中執(zhí)行線程函數(shù)。

在Linux中,通過函數(shù)pthread_create()函數(shù)實(shí)現(xiàn)線程的創(chuàng)建:

pthread_create()

  1. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*st 

其中:

  • thread表示的是一個(gè)pthread_t類型的指針;
  • attr用于指定線程的一些屬性;
  • start_routine表示的是一個(gè)函數(shù)指針,該函數(shù)是線程調(diào)用函數(shù);
  • arg表示的是傳遞給線程調(diào)用函數(shù)的參數(shù)。

當(dāng)線程創(chuàng)建成功時(shí),函數(shù)pthread_create()返回0,若返回值不為0則表示創(chuàng)建線程失敗。對(duì)于線程的屬性,則在結(jié)構(gòu)體pthread_attr_t中定義。

線程創(chuàng)建的過程如下所示:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. #include <malloc.h> 
  5.  
  6. void* thread(void *id){ 
  7.    pthread_t newthid; 
  8.  
  9.    newthid = pthread_self(); 
  10.    printf("this is a new thread, thread ID is %u\n", newthid); 
  11.    return NULL
  12.  
  13. int main(){ 
  14.  int num_thread = 5; 
  15.  pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  16.  
  17.  printf("main thread, ID is %u\n", pthread_self()); 
  18.  for (int i = 0; i < num_thread; i++){ 
  19.        if (pthread_create(&pt[i], NULL, thread, NULL) != 0){ 
  20.           printf("thread create failed!\n"); 
  21.           return 1; 
  22.        } 
  23.  } 
  24.  sleep(2); 
  25.  free(pt); 
  26.  return 0; 

在上述代碼中,使用到了pthread_self()函數(shù),該函數(shù)的作用是獲取本線程的線程ID。在主函數(shù)中的sleep()用于將主進(jìn)程處于等待狀態(tài),以讓線程執(zhí)行完成。最終的執(zhí)行效果如下所示:

 

那么,如何利用arg向子線程傳遞參數(shù)呢?其具體的實(shí)現(xiàn)如下所示:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. #include <malloc.h> 
  5.  
  6. void* thread(void *id){ 
  7.   pthread_t newthid; 
  8.  
  9.   newthid = pthread_self(); 
  10.   int num = *(int *)id; 
  11.   printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
  12.   return NULL
  13.  
  14. int main(){ 
  15.   //pthread_t thid; 
  16.   int num_thread = 5; 
  17.   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  18.   int * id = (int *)malloc(sizeof(int) * num_thread); 
  19.  
  20.   printf("main thread, ID is %u\n", pthread_self()); 
  21.   for (int i = 0; i < num_thread; i++){ 
  22.      id[i] = i; 
  23.      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
  24.         printf("thread create failed!\n"); 
  25.         return 1; 
  26.      } 
  27.   } 
  28.   sleep(2); 
  29.   free(pt); 
  30.   free(id); 
  31.   return 0; 

其最終的執(zhí)行效果如下圖所示:

 

如果在主進(jìn)程提前結(jié)束,會(huì)出現(xiàn)什么情況呢?如下述的代碼:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. #include <malloc.h> 
  5.  
  6. void* thread(void *id){ 
  7.   pthread_t newthid; 
  8.  
  9.   newthid = pthread_self(); 
  10.   int num = *(int *)id; 
  11.   printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
  12.   sleep(2); 
  13.   printf("thread %u is done!\n", newthid); 
  14.   return NULL
  15.  
  16. int main(){ 
  17.   //pthread_t thid; 
  18.   int num_thread = 5; 
  19.   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  20.   int * id = (int *)malloc(sizeof(int) * num_thread); 
  21.  
  22.   printf("main thread, ID is %u\n", pthread_self()); 
  23.   for (int i = 0; i < num_thread; i++){ 
  24.      id[i] = i; 
  25.      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
  26.         printf("thread create failed!\n"); 
  27.         return 1; 
  28.      } 
  29.    } 
  30.    //sleep(2); 
  31.    free(pt); 
  32.    free(id); 
  33.    return 0; 

此時(shí),主進(jìn)程提前結(jié)束,進(jìn)程會(huì)將資源回收,此時(shí),線程都將退出執(zhí)行,運(yùn)行結(jié)果如下所示:

 

線程掛起

在上述的實(shí)現(xiàn)過程中,為了使得主線程能夠等待每一個(gè)子線程執(zhí)行完成后再退出,使用了free()函數(shù),在Linux的多線程中,也可以使用pthread_join()函數(shù)用于等待其他線程,函數(shù)的具體形式為:

  1. int pthread_join(pthread_t thread, void **retval); 

函數(shù)pthread_join()用來等待一個(gè)線程的結(jié)束,其調(diào)用這將被掛起。

一個(gè)線程僅允許一個(gè)線程使用pthread_join()等待它的終止。

如需要在主線程中等待每一個(gè)子線程的結(jié)束,如下述代碼所示:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <unistd.h> 
  4. #include <malloc.h> 
  5.  
  6. void* thread(void *id){ 
  7.   pthread_t newthid; 
  8.  
  9.   newthid = pthread_self(); 
  10.   int num = *(int *)id; 
  11.   printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
  12.  
  13.   printf("thread %u is done\n", newthid); 
  14.   return NULL
  15.  
  16. int main(){ 
  17.    int num_thread = 5; 
  18.    pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  19.    int * id = (int *)malloc(sizeof(int) * num_thread); 
  20.  
  21.    printf("main thread, ID is %u\n", pthread_self()); 
  22.    for (int i = 0; i < num_thread; i++){ 
  23.       id[i] = i; 
  24.       if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
  25.          printf("thread create failed!\n"); 
  26.          return 1; 
  27.        } 
  28.    } 
  29.    for (int i = 0; i < num_thread; i++){ 
  30.       pthread_join(pt[i], NULL); 
  31.    } 
  32.    free(pt); 
  33.    free(id); 
  34.    return 0; 

最終的執(zhí)行效果如下所示:

 

 

注:在編譯的時(shí)候需要鏈接libpthread.a:

g++ xx.c -lpthread -o xx

互斥鎖mutex

多線程的問題引入

多線程的最大的特點(diǎn)是資源的共享,但是,當(dāng)多個(gè)線程同時(shí)去操作(同時(shí)去改變)一個(gè)臨界資源時(shí),會(huì)破壞臨界資源。如利用多線程同時(shí)寫一個(gè)文件:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <malloc.h> 
  4.  
  5. const char filename[] = "hello"
  6.  
  7. void* thread(void *id){ 
  8.   int num = *(int *)id; 
  9.  
  10.   // 寫文件的操作 
  11.   FILE *fp = fopen(filename, "a+"); 
  12.   int start = *((int *)id); 
  13.   int end = start + 1; 
  14.   setbuf(fp, NULL);// 設(shè)置緩沖區(qū)的大小 
  15.   fprintf(stdout, "%d\n", start); 
  16.   for (int i = (start * 10); i < (end * 10); i ++){ 
  17.       fprintf(fp, "%d\t", i); 
  18.   } 
  19.   fprintf(fp, "\n"); 
  20.   fclose(fp); 
  21.   return NULL
  22.  
  23. int main(){ 
  24.    int num_thread = 5; 
  25.    pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  26.    int * id = (int *)malloc(sizeof(int) * num_thread); 
  27.  
  28.    for (int i = 0; i < num_thread; i++){ 
  29.       id[i] = i; 
  30.       if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
  31.          printf("thread create failed!\n"); 
  32.          return 1; 
  33.          } 
  34.    } 
  35.    for (int i = 0; i < num_thread; i++){ 
  36.       pthread_join(pt[i], NULL); 
  37.    } 
  38.    // 釋放資源 
  39.    free(pt); 
  40.    free(id); 
  41.    return 0; 

執(zhí)行以上的代碼,我們會(huì)發(fā)現(xiàn),得到的結(jié)果是混亂的,出現(xiàn)上述的最主要的原因是,我們?cè)诰帉懚嗑€程代碼的過程中,每一個(gè)線程都嘗試去寫同一個(gè)文件,這樣便出現(xiàn)了上述的問題,這便是共享資源的同步問題,在Linux編程中,線程同步的處理方法包括:信號(hào)量,互斥鎖和條件變量。

 

互斥鎖

互斥鎖是通過鎖的機(jī)制來實(shí)現(xiàn)線程間的同步問題?;コ怄i的基本流程為:

  • 初始化一個(gè)互斥鎖:pthread_mutex_init()函數(shù)
  • 加鎖:pthread_mutex_lock()函數(shù)或者pthread_mutex_trylock()函數(shù)
  • 對(duì)共享資源的操作
  • 解鎖:pthread_mutex_unlock()函數(shù)
  • 注銷互斥鎖:pthread_mutex_destory()函數(shù)

其中,在加鎖過程中,pthread_mutex_lock()函數(shù)和pthread_mutex_trylock()函數(shù)的過程略有不同:

  • 當(dāng)使用pthread_mutex_lock()函數(shù)進(jìn)行加鎖時(shí),若此時(shí)已經(jīng)被鎖,則嘗試加鎖的線程會(huì)被阻塞,直到互斥鎖被其他線程釋放,當(dāng)pthread_mutex_lock()函數(shù)有返回值時(shí),說明加鎖成功;
  • 而使用pthread_mutex_trylock()函數(shù)進(jìn)行加鎖時(shí),若此時(shí)已經(jīng)被鎖,則會(huì)返回EBUSY的錯(cuò)誤碼。

同時(shí),解鎖的過程中,也需要滿足兩個(gè)條件:

  • 解鎖前,互斥鎖必須處于鎖定狀態(tài);
  • 必須由加鎖的線程進(jìn)行解鎖。

當(dāng)互斥鎖使用完成后,必須進(jìn)行清除。

有了以上的準(zhǔn)備,我們重新實(shí)現(xiàn)上述的多線程寫操作,其實(shí)現(xiàn)代碼如下所示:

  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <malloc.h> 
  4.  
  5. pthread_mutex_t mutex; 
  6.  
  7. const char filename[] = "hello"
  8.  
  9. void* thread(void *id){ 
  10.  
  11.    int num = *(int *)id; 
  12.    // 加鎖 
  13.  
  14.    if (pthread_mutex_lock(&mutex) != 0){ 
  15.      fprintf(stdout, "lock error!\n"); 
  16.    } 
  17.    // 寫文件的操作 
  18.    FILE *fp = fopen(filename, "a+"); 
  19.    int start = *((int *)id); 
  20.    int end = start + 1; 
  21.    setbuf(fp, NULL);// 設(shè)置緩沖區(qū)的大小 
  22.    fprintf(stdout, "%d\n", start); 
  23.    for (int i = (start * 10); i < (end * 10); i ++){ 
  24.       fprintf(fp, "%d\t", i); 
  25.    } 
  26.    fprintf(fp, "\n"); 
  27.    fclose(fp); 
  28.  
  29.    // 解鎖 
  30.    pthread_mutex_unlock(&mutex); 
  31.    return NULL
  32.  
  33. int main(){ 
  34.    int num_thread = 5; 
  35.    pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  36.    int * id = (int *)malloc(sizeof(int) * num_thread); 
  37.  
  38.    // 初始化互斥鎖 
  39.    if (pthread_mutex_init(&mutex, NULL) != 0){ 
  40.      // 互斥鎖初始化失敗 
  41.      free(pt); 
  42.      free(id); 
  43.      return 1; 
  44.    } 
  45.    for (int i = 0; i < num_thread; i++){ 
  46.       id[i] = i; 
  47.       if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
  48.          printf("thread create failed!\n"); 
  49.          return 1; 
  50.       } 
  51.    } 
  52.    for (int i = 0; i < num_thread; i++){ 
  53.       pthread_join(pt[i], NULL); 
  54.    } 
  55.    pthread_mutex_destroy(&mutex); 
  56.    // 釋放資源 
  57.    free(pt); 
  58.    free(id); 
  59.    return 0; 

最終的結(jié)果為:

 

參考文章:

http://www.broadview.com.cn/article/297

https://www.cnblogs.com/jingzhishen/p/3807455.html

本文轉(zhuǎn)載自微信公眾號(hào)「一口Linux」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系一口Linux公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 一口Linux
相關(guān)推薦

2025-02-17 02:00:00

Monitor機(jī)制代碼

2024-06-28 08:45:58

2024-06-24 08:10:00

C++互斥鎖

2009-11-28 20:24:13

Linux互斥鎖同步移植

2023-06-02 08:29:24

https://wwMutex

2023-12-24 12:33:20

互斥鎖Go代碼

2020-09-04 10:14:02

Linux驅(qū)動(dòng)7內(nèi)核

2020-09-16 07:56:28

多線程讀寫鎖悲觀鎖

2024-07-25 11:53:53

2024-10-14 08:51:52

協(xié)程Go語言

2020-09-03 11:10:34

MySQL數(shù)據(jù)庫

2020-11-13 07:16:09

線程互斥鎖死循環(huán)

2021-03-24 08:02:58

C語言

2024-03-04 00:20:00

C#線程代碼

2021-05-25 09:28:34

鴻蒙HarmonyOS應(yīng)用

2024-03-07 07:47:04

代碼塊Monitor

2025-05-06 08:20:00

互斥鎖C++編程

2023-05-17 15:07:42

智能開發(fā)鴻蒙

2017-06-02 08:48:29

互斥鎖JavaCAS

2022-06-27 08:07:13

Go語言互斥鎖
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 欧美激情精品久久久久久免费 | 9久久| 亚洲一区二区三区免费视频 | www亚洲一区 | 天天搞夜夜操 | 午夜欧美日韩 | 夜夜夜夜夜夜曰天天天 | 婷婷成人在线 | 日日骚视频| 大学生a级毛片免费视频 | 精品国产伦一区二区三区观看体验 | 久久久久九九九女人毛片 | 日本视频免费观看 | 国产91久久久久久久免费 | 一区二区三区国产精品 | 91久久精品国产91久久 | 免费观看成人性生生活片 | 91精品国产一区二区三区 | 午夜小视频在线播放 | 日本成人在线网址 | 亚洲一区中文字幕在线观看 | 成人精品鲁一区一区二区 | 成人午夜高清 | 九色91视频| 精品久久久久久亚洲综合网站 | 亚洲一区久久 | 日本中出视频 | 91精品国产高清久久久久久久久 | 亚洲综合色站 | 国产不卡视频在线 | 欧美黄a | 三级视频在线观看电影 | 欧美日韩不卡在线 | 日韩一区二区三区在线看 | 免费观看一级毛片 | 免费v片| 狠狠色狠狠色综合日日92 | 在线午夜 | 日本一区二区三区四区 | 韩国理论电影在线 | 成人一区二区三区在线观看 |