Linux編程練習 –多線程3–mutex

來源:互聯網
上載者:User

互斥指互斥的鎖,是一種訊號量,常用來防止兩個進程或線程在同一時刻訪問相同的共用資源

        

1.資料類型:

 在Linux下, 線程的互斥量資料類型是pthread_mutex_t,我們定義一個互斥資料可以這樣:

              pthread_mutex_t mutex;

 

2.函數說明:
標頭檔:     pthread.h

(1).互斥鎖初始化:

 

函數原型: int pthread_mutex_init (pthread_mutex_t* mutex,

                                 const pthread_mutexattr_t*   mutexattr);
函數傳入值:  mutex:互斥鎖。

                mutexattr:PTHREAD_MUTEX_INITIALIZER:建立快速互斥鎖。

                PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:建立遞迴互斥鎖。

                   PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:建立檢錯互斥鎖。

函數傳回值: 成功:0

                   出錯:-1 
(2).互斥操作函數

int pthread_mutex_lock(pthread_mutex_t* mutex);//上鎖

int pthread_mutex_trylock (pthread_mutex_t* mutex);//只有在互斥被鎖住的情況下才阻塞

int pthread_mutex_unlock (pthread_mutex_t* mutex);//解鎖

int pthread_mutex_destroy (pthread_mutex_t* mutex);//清除互斥鎖

函數傳入值:            mutex:互斥鎖。

函數傳回值:           成功:0

                              出錯:-1

3.使用形式:

struct mutex mutex;

mutex_init(&mutex); /*定義*/

...
mutex_lock(&mutex); /*擷取互斥鎖*/

... /*臨界資源*/

mutex_unlock(&mutex); /*釋放互斥鎖*/

 

 

最後進行一個練習:我們建立兩個線程,分別訪問全域變數gnum,並且修改它,列印出來

[cpp] view plaincopy

  1. /*mutex.c*/  
  2. #include <stdlib.h>    
  3. #include <stdio.h>    
  4. #include <pthread.h>    
  5. #include <errno.h>     
  6. /*全域變數*/  
  7. int gnum = 0;  
  8. /*互斥量 */  
  9. pthread_mutex_t mutex;  
  10. /*聲明線程運行服務程式*/  
  11. static void pthread_func_1 (void);     
  12. static void pthread_func_2 (void);     
  13.     
  14. int main (void)     
  15. {     
  16.  /*線程的標識符*/  
  17.   pthread_t pt_1 = 0;     
  18.   pthread_t pt_2 = 0;     
  19.   int ret = 0;     
  20.   /*互斥初始化*/  
  21.   pthread_mutex_init (&mutex, NULL);   
  22.   /*分別建立線程1、2*/  
  23.   ret = pthread_create (&pt_1,          //線程標識符指標  
  24.                          NULL,          //預設屬性  
  25.                         (void *)pthread_func_1,//運行函數  
  26.                         NULL);          //無參數  
  27.   if (ret != 0)     
  28.   {     
  29.      perror ("pthread_1_create");     
  30.   }     
  31.     
  32.   ret = pthread_create (&pt_2,          //線程標識符指標  
  33.                         NULL,           //預設屬性    
  34.                         (void *)pthread_func_2, //運行函數  
  35.                         NULL);          //無參數  
  36.   if (ret != 0)     
  37.   {     
  38.      perror ("pthread_2_create");     
  39.   }     
  40.   /*等待線程1、2的結束*/  
  41.   pthread_join (pt_1, NULL);     
  42.   pthread_join (pt_2, NULL);     
  43.     
  44.   printf ("main programme exit!/n");    
  45.   return 0;     
  46. }     
  47. /*線程1的服務程式*/  
  48. static void pthread_func_1 (void)     
  49. {     
  50.   int i = 0;     
  51.        
  52.   for (;;)     
  53.   {     
  54.     printf ("This is pthread1!/n");      
  55.     pthread_mutex_lock(&mutex); /*擷取互斥鎖*/  
  56.     /*注意,這裡以防線程的搶佔,以造成一個線程在另一個線程sleep時多次訪問互斥資源,所以sleep要在得到互斥鎖後調用*/  
  57.     sleep (1);    
  58.     /*臨界資源*/  
  59.     gnum++;  
  60.     printf ("Thread1 add one to num:%d/n",gnum);  
  61.     pthread_mutex_unlock(&mutex); /*釋放互斥鎖*/  
  62.   
  63.        
  64.   }     
  65. }     
  66. /*線程2的服務程式*/   
  67. static void pthread_func_2 (void)     
  68. {     
  69.   int i = 0;     
  70.     
  71.   for (;;)     
  72.   {     
  73.     printf ("This is pthread2!/n");   
  74.     pthread_mutex_lock(&mutex); /*擷取互斥鎖*/  
  75.     /*注意,這裡以防線程的搶佔,以造成一個線程在另一個線程sleep時多次訪問互斥資源,所以sleep要在得到互斥鎖後調用*/  
  76.     sleep (1);  
  77.     /*臨界資源*/  
  78.     gnum++;  
  79.     printf ("Thread2 add one to num:%d/n",gnum);  
  80.     pthread_mutex_unlock(&mutex); /*釋放互斥鎖*/  
  81.     
  82.   }     
  83.     
  84.   pthread_exit (0);     
  85. }    

然後編譯,運行,看到是線程1,2分別和平地訪問共用資源

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.