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

Linux互斥鎖之線程互斥鎖

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

 [[339322]]

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

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

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

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

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

作用域

信號量 : 進程間或線程間(linux僅線程間)

互斥鎖 : 線程間

上鎖時

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

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

多線程

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

 

線程創(chuàng)建

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

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

pthread_create()

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

其中:

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

當線程創(chuàng)建成功時,函數(shù)pthread_create()返回0,若返回值不為0則表示創(chuàng)建線程失敗。對于線程的屬性,則在結(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()用于將主進程處于等待狀態(tài),以讓線程執(zhí)行完成。最終的執(zhí)行效果如下所示:

 

那么,如何利用arg向子線程傳遞參數(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í)行效果如下圖所示:

 

如果在主進程提前結(jié)束,會出現(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; 

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

 

線程掛起

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

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

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

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

如需要在主線程中等待每一個子線程的結(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í)行效果如下所示:

 

 

注:在編譯的時候需要鏈接libpthread.a:

g++ xx.c -lpthread -o xx

互斥鎖mutex

多線程的問題引入

多線程的最大的特點是資源的共享,但是,當多個線程同時去操作(同時去改變)一個臨界資源時,會破壞臨界資源。如利用多線程同時寫一個文件:

  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í)行以上的代碼,我們會發(fā)現(xiàn),得到的結(jié)果是混亂的,出現(xiàn)上述的最主要的原因是,我們在編寫多線程代碼的過程中,每一個線程都嘗試去寫同一個文件,這樣便出現(xiàn)了上述的問題,這便是共享資源的同步問題,在Linux編程中,線程同步的處理方法包括:信號量,互斥鎖和條件變量。

 

互斥鎖

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

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

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

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

同時,解鎖的過程中,也需要滿足兩個條件:

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

當互斥鎖使用完成后,必須進行清除。

有了以上的準備,我們重新實現(xiàn)上述的多線程寫操作,其實現(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)載自微信公眾號「一口Linux」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系一口Linux公眾號。

 

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

2025-02-17 02:00:00

Monitor機制代碼

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ū)動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語言互斥鎖
點贊
收藏

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

主站蜘蛛池模板: 日本在线看片 | 成人av高清 | 日韩欧美三级电影 | 性高湖久久久久久久久aaaaa | 影音先锋亚洲资源 | 天天干夜夜拍 | 欧美性猛交一区二区三区精品 | 欧美日韩专区 | 亚洲二区在线 | 亚洲免费网址 | 麻豆国产一区二区三区四区 | 国产免费高清 | 欧美日韩不卡合集视频 | 欧美日韩在线一区二区 | 国产成人免费网站 | 亚洲一区二区在线播放 | 91小视频| 欧美成人a∨高清免费观看 欧美日韩中 | 玖玖色在线视频 | 欧美成人精品欧美一级 | 粉色午夜视频 | 国产成人自拍一区 | 欧美视频第三页 | 午夜视频在线免费观看 | 精品国产一区二区三区观看不卡 | 欧美三级电影在线播放 | 亚洲精品久久久久久久久久久久久 | 国产成人99久久亚洲综合精品 | 欧美美女一区二区 | 日韩午夜在线播放 | 成人在线视频网 | 久久久av | 久久久精品一区 | 久久国产精品一区二区三区 | 成人免费区一区二区三区 | 久久久99精品免费观看 | 男女羞羞视频免费看 | 日韩欧美一级片 | 中文在线一区 | 欧美日韩中文字幕在线播放 | 色综合视频 |