首頁 > 系統教程 > Linux > 主體

Linux多執行緒編程鎖詳解:如何避免競爭與死鎖

WBOY
發布: 2024-02-11 16:30:22
轉載
573 人瀏覽過

在Linux多執行緒程式設計中,鎖是一種非常重要的機制,可以避免執行緒間的競爭和死鎖。然而,如果不正確使用鎖,可能會導致效能下降和不穩定的行為。本文將介紹Linux中常見的鎖定類型,如何正確使用它們,以及如何避免競爭和死鎖等問題。

在程式設計中,引入了物件互斥鎖的概念,來保證共享資料操作的完整性。每個物件都對應到一個可稱為” 互斥鎖” 的標記,這個標記用來保證在任一時刻,只能有一個執行緒存取該物件。 Linux實現的互斥鎖機制包括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  
#include  
 
void* 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 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多執行緒編程鎖詳解:如何避免競爭與死鎖

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

#include  
#include  
#include  
#include  
 
void* 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 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多執行緒編程鎖詳解:如何避免競爭與死鎖

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

#include  
#include  
#include  
#include  
 
void* 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 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多執行緒編程鎖詳解:如何避免競爭與死鎖

執行緒掛起

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

int pthread_join(pthread_t thread, void **retval); 
登入後複製

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

一个线程仅允许一个线程使用pthread_join()等待它的终止。

如需要在主线程中等待每一个子线程的结束,如下述代码所示:

#include  
#include  
#include  
#include  
 
void* 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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
         printf("thread create failed!\n"); 
         return 1; 
       } 
   } 
   for (int i = 0; i return 0; 
} 
登入後複製

最终的执行效果如下所示:

Linux多執行緒編程鎖詳解:如何避免競爭與死鎖

注:在编译的时候需要链接libpthread.a:

g++ xx.c -lpthread -o xx

互斥锁mutex

多线程的问题引入

多线程的最大的特点是资源的共享,但是,当多个线程同时去操作(同时去改变)一个临界资源时,会破坏临界资源。如利用多线程同时写一个文件:

#include  
#include  
 
const 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 "%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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
         printf("thread create failed!\n"); 
         return 1; 
         } 
   } 
   for (int i = 0; i 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  
#include  
 
pthread_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 "%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 if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ 
         printf("thread create failed!\n"); 
         return 1; 
      } 
   } 
   for (int i = 0; i return 0; 
} 
登入後複製

最终的结果为:

Linux多執行緒編程鎖詳解:如何避免競爭與死鎖

通过本文的介绍,您应该已经了解了Linux多线程编程中的常见锁类型、正确使用锁的方法以及如何避免竞争和死锁等问题。锁机制是多线程编程中必不可少的一部分,掌握它们可以使您的代码更加健壮和可靠。在实际应用中,应该根据具体情况选择合适的锁类型,并遵循最佳实践,以确保程序的高性能和可靠性。

以上是Linux多執行緒編程鎖詳解:如何避免競爭與死鎖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:lxlinux.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!