Linux之執行緒互斥鎖

安全劍客發表於2020-10-08
導讀 在程式設計中,引入了物件互斥鎖的概念,來保證共享資料操作的完整性。每個物件都對應於一個可稱為" 互斥鎖" 的標記,這個標記用來保證在任一時刻,只能有一個執行緒訪問該物件。 實現的互斥鎖機制包括POSIX互斥鎖和核心互斥鎖,本文主要講POSIX互斥鎖,即執行緒間互斥鎖。

訊號量用在多執行緒多工同步的,一個執行緒完成了某一個動作就透過訊號量告訴別的執行緒,別的執行緒再進行某些動作(大家都在sem_wait的時候,就阻塞在 那裡)。而互斥鎖是用在多執行緒多工互斥的,一個執行緒佔用了某一個資源,那麼別的執行緒就無法訪問,直到這個執行緒unlock,其他的執行緒才開始可以利用這 個資源。比如對全域性變數的訪問,有時要加鎖,操作完了,在解鎖。有的時候鎖和訊號量會同時使用的”

也就是說,訊號量不一定是鎖定某一個資源,而是 流程上的概念,比如:有A,B兩個執行緒,B執行緒要等A執行緒完成某一任務以後再進行自己下面的步驟,這個任務並不一定是鎖定某一資源,還可以是進行一些計算 或者資料處理之類。而執行緒互斥量則是“鎖住某一資源”的概念,在鎖定期間內,其他執行緒無法對被保護的資料進行操作。在有些情況下兩者可以互換。

兩者之間的區別:

作用域

訊號量 : 程式間或執行緒間(linux僅執行緒間)

互斥鎖 : 執行緒間

上鎖時

訊號量 : 只要訊號量的value大於0,其他執行緒就可以sem_wait成功,成功後訊號量的value減一。若value值不大於0,則sem_wait阻塞,直到sem_post釋放後value值加一。一句話,訊號量的value>=0 。

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

多執行緒

執行緒是計算機中獨立執行的最小單位,執行時佔用很少的系統資源。與多程式相比,多程式具有多程式不具備的一些優點,其最重要的是:對於多執行緒來說,其能夠比多程式更加節省資源。

執行緒建立

在Linux中,新建的執行緒並不是在原先的程式中,而是系統透過一個系統呼叫clone()。該系統copy了一個和原先程式完全一樣的程式,並在這個程式中執行執行緒函式。

在Linux中,透過函式pthread_create()函式實現執行緒的建立:

pthread_create()

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

其中:

  • thread表示的是一個pthread_t型別的指標;
  • attr用於指定執行緒的一些屬性;
  • start_routine表示的是一個函式指標,該函式是執行緒呼叫函式;
  • arg表示的是傳遞給執行緒呼叫函式的引數。
  • 當執行緒建立成功時,函式pthread_create()返回0,若返回值不為0則表示建立執行緒失敗。對於執行緒的屬性,則在結構體pthread_attr_t中定義。

    執行緒建立的過程如下所示:

#include#include#include#includevoid* thread(void *id){ 
   pthread_t newthid; 
 
   newthid = pthread_self(); 
   printf("this is a new thread, thread ID is %u\n", newthid); 
   return NULL; 
} 
 
int main(){ 
 int num_thread = 5; 
 pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
 
 printf("main thread, ID is %u\n", pthread_self()); 
 for (int i = 0; i < num_thread; i++){ 
       if (pthread_create(&pt[i], NULL, thread, NULL) != 0){ 
          printf("thread create failed!\n"); 
          return 1; 
       } 
 } 
 sleep(2); 
 free(pt); 
 return 0; 
}

在上述程式碼中,使用到了pthread_self()函式,該函式的作用是獲取本執行緒的執行緒ID。在主函式中的sleep()用於將主程式處於等待狀態,以讓執行緒執行完成。最終的執行效果如下所示:

Linux之執行緒互斥鎖Linux之執行緒互斥鎖

那麼,如何利用arg向子執行緒傳遞引數呢?其具體的實現如下所示:

#include#include#include#includevoid* thread(void *id){ 
  pthread_t newthid; 
 
  newthid = pthread_self(); 
  int num = *(int *)id; 
  printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
  return NULL; 
} 
int main(){ 
  //pthread_t thid; 
  int num_thread = 5; 
  pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  int * id = (int *)malloc(sizeof(int) * num_thread); 
 
  printf("main thread, ID is %u\n", pthread_self()); 
  for (int i = 0; i < num_thread; i++){ 
     id[i] = i; 
     if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
        printf("thread create failed!\n"); 
        return 1; 
     } 
  } 
  sleep(2); 
  free(pt); 
  free(id); 
  return 0; 
}

其最終的執行效果如下圖所示:

Linux之執行緒互斥鎖Linux之執行緒互斥鎖

如果在主程式提前結束,會出現什麼情況呢?如下述的程式碼:

#include#include#include#includevoid* thread(void *id){ 
  pthread_t newthid; 
 
  newthid = pthread_self(); 
  int num = *(int *)id; 
  printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
  sleep(2); 
  printf("thread %u is done!\n", newthid); 
  return NULL; 
} 
 
int main(){ 
  //pthread_t thid; 
int num_thread = 5; 
  pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
  int * id = (int *)malloc(sizeof(int) * num_thread); 
 
  printf("main thread, ID is %u\n", pthread_self()); 
  for (int i = 0; i < num_thread; i++){ 
     id[i] = i; 
     if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
        printf("thread create failed!\n"); 
        return 1; 
     } 
   } 
   //sleep(2); 
   free(pt); 
   free(id); 
   return 0; 
}

此時,主程式提前結束,程式會將資源回收,此時,執行緒都將退出執行,執行結果如下所示:

Linux之執行緒互斥鎖Linux之執行緒互斥鎖

執行緒掛起

在上述的實現過程中,為了使得主執行緒能夠等待每一個子執行緒執行完成後再退出,使用了free()函式,在Linux的多執行緒中,也可以使用pthread_join()函式用於等待其他執行緒,函式的具體形式為:

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

函式pthread_join()用來等待一個執行緒的結束,其呼叫這將被掛起。

一個執行緒僅允許一個執行緒使用pthread_join()等待它的終止。

如需要在主執行緒中等待每一個子執行緒的結束,如下述程式碼所示:

#include#include#include#includevoid* thread(void *id){ 
  pthread_t newthid; 
 
  newthid = pthread_self(); 
  int num = *(int *)id; 
  printf("this is a new thread, thread ID is %u,id:%d\n", newthid, num); 
 
  printf("thread %u is done\n", newthid); 
  return NULL; 
} 
int main(){ 
   int num_thread = 5; 
   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
   int * id = (int *)malloc(sizeof(int) * num_thread); 
 
   printf("main thread, ID is %u\n", pthread_self()); 
   for (int i = 0; i < num_thread; i++){ 
      id[i] = i; 
      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
         printf("thread create failed!\n"); 
         return 1; 
       } 
   } 
   for (int i = 0; i < num_thread; i++){ 
      pthread_join(pt[i], NULL); 
   } 
   free(pt); 
   free(id); 
return 0; 
}

最終的執行效果如下所示:

Linux之執行緒互斥鎖Linux之執行緒互斥鎖

注:在編譯的時候需要連結libpthread.a:

g++ xx.c -lpthread -o xx

互斥鎖mutex
多執行緒的問題引入

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

#include#includeconst char filename[] = "hello"; 
 
void* thread(void *id){ 
  int num = *(int *)id; 
 
  // 寫檔案的操作 
  FILE *fp = fopen(filename, "a+"); 
  int start = *((int *)id); 
  int end = start + 1; 
  setbuf(fp, NULL);// 設定緩衝區的大小 
  fprintf(stdout, "%d\n", start); 
  for (int i = (start * 10); i < (end * 10); i ++){ 
      fprintf(fp, "%d\t", i); 
  } 
  fprintf(fp, "\n"); 
  fclose(fp); 
  return NULL; 
} 
 
int main(){ 
   int num_thread = 5; 
   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
   int * id = (int *)malloc(sizeof(int) * num_thread); 
 
   for (int i = 0; i < num_thread; i++){ 
      id[i] = i; 
      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
         printf("thread create failed!\n"); 
         return 1; 
         } 
   } 
   for (int i = 0; i < num_thread; i++){ 
      pthread_join(pt[i], NULL); 
   } 
   // 釋放資源 
   free(pt); 
free(id); 
   return 0; 
}

執行以上的程式碼,我們會發現,得到的結果是混亂的,出現上述的最主要的原因是,我們在編寫多執行緒程式碼的過程中,每一個執行緒都嘗試去寫同一個檔案,這樣便出現了上述的問題,這便是共享資源的同步問題,在Linux程式設計中,執行緒同步的處理方法包括:訊號量,互斥鎖和條件變數。

互斥鎖

互斥鎖是透過鎖的機制來實現執行緒間的同步問題。互斥鎖的基本流程為:

  • 初始化一個互斥鎖:pthread_mutex_init()函式
  • 加鎖:pthread_mutex_lock()函式或者pthread_mutex_trylock()函式
  • 對共享資源的操作
  • 解鎖:pthread_mutex_unlock()函式
  • 登出互斥鎖:pthread_mutex_destory()函式
  • 其中,在加鎖過程中,pthread_mutex_lock()函式和pthread_mutex_trylock()函式的過程略有不同:

  • 當使用pthread_mutex_lock()函式進行加鎖時,若此時已經被鎖,則嘗試加鎖的執行緒會被阻塞,直到互斥鎖被其他執行緒釋放,當pthread_mutex_lock()函式有返回值時,說明加鎖成功;
  • 而使用pthread_mutex_trylock()函式進行加鎖時,若此時已經被鎖,則會返回EBUSY的錯誤碼。
  • 同時,解鎖的過程中,也需要滿足兩個條件:

  • 解鎖前,互斥鎖必須處於鎖定狀態;
  • 必須由加鎖的執行緒進行解鎖。
  • 當互斥鎖使用完成後,必須進行清除。

    有了以上的準備,我們重新實現上述的多執行緒寫操作,其實現程式碼如下所示:

    #include#includepthread_mutex_t mutex; 
     
    const char filename[] = "hello"; 
     
    void* thread(void *id){ 
     
       int num = *(int *)id; 
       // 加鎖 
     
       if (pthread_mutex_lock(&mutex) != 0){ 
         fprintf(stdout, "lock error!\n"); 
       } 
       // 寫檔案的操作 
       FILE *fp = fopen(filename, "a+"); 
       int start = *((int *)id); 
       int end = start + 1; 
       setbuf(fp, NULL);// 設定緩衝區的大小 
    fprintf(stdout, "%d\n", start); 
       for (int i = (start * 10); i < (end * 10); i ++){ 
          fprintf(fp, "%d\t", i); 
       } 
       fprintf(fp, "\n"); 
       fclose(fp); 
     
       // 解鎖 
       pthread_mutex_unlock(&mutex); 
       return NULL; 
    } 
     
    int main(){ 
       int num_thread = 5; 
       pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); 
       int * id = (int *)malloc(sizeof(int) * num_thread); 
     // 初始化互斥鎖 
       if (pthread_mutex_init(&mutex, NULL) != 0){ 
         // 互斥鎖初始化失敗 
         free(pt); 
         free(id); 
         return 1; 
       } 
       for (int i = 0; i < num_thread; i++){ 
          id[i] = i; 
          if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
             printf("thread create failed!\n"); 
             return 1; 
          } 
       } 
       for (int i = 0; i < num_thread; i++){ 
          pthread_join(pt[i], NULL); 
       } 
       pthread_mutex_destroy(&mutex); 
       // 釋放資源 
     free(pt); 
       free(id); 
       return 0; 
    }

    最終的結果為:

    Linux之執行緒互斥鎖Linux之執行緒互斥鎖

    原文來自: 


    來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2725667/,如需轉載,請註明出處,否則將追究法律責任。

    相關文章