【轉】 Linux 線程同步的三種方法


線程的最大特點是資源的共享性,但資源共享中的同步問題是多線程編程的難點。linux下提供了多種方式來處理線程同步,最常用的是互斥鎖、條件變量和信號量。

一、互斥鎖(mutex)

通過鎖機制實現線程間的同步。

  1. 初始化鎖。在Linux下,線程的互斥量數據類型是pthread_mutex_t。在使用前,要對它進行初始化。
    靜態分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    動態分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);
  2. 加鎖。對共享資源的訪問,要對互斥量進行加鎖,如果互斥量已經上了鎖,調用線程會阻塞,直到互斥量被解鎖。
    int pthread_mutex_lock(pthread_mutex *mutex);
    int pthread_mutex_trylock(pthread_mutex_t *mutex);
  3. 解鎖。在完成了對共享資源的訪問后,要對互斥量進行解鎖。
    int pthread_mutex_unlock(pthread_mutex_t *mutex);
  4. 銷毀鎖。鎖在是使用完成后,需要進行銷毀以釋放資源。
    int pthread_mutex_destroy(pthread_mutex *mutex);
[csharp]  view plain  copy
 
  1. #include <cstdio>  
  2. #include <cstdlib>  
  3. #include <unistd.h>  
  4. #include <pthread.h>  
  5. #include "iostream"  
  6. using namespace std;  
  7. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  8. int tmp;  
  9. void* thread(void *arg)  
  10. {  
  11.     cout << "thread id is " << pthread_self() << endl;  
  12.     pthread_mutex_lock(&mutex);  
  13.     tmp = 12;  
  14.     cout << "Now a is " << tmp << endl;  
  15.     pthread_mutex_unlock(&mutex);  
  16.     return NULL;  
  17. }  
  18. int main()  
  19. {  
  20.     pthread_t id;  
  21.     cout << "main thread id is " << pthread_self() << endl;  
  22.     tmp = 3;  
  23.     cout << "In main func tmp = " << tmp << endl;  
  24.     if (!pthread_create(&id, NULL, thread, NULL))  
  25.     {  
  26.         cout << "Create thread success!" << endl;  
  27.     }  
  28.     else  
  29.     {  
  30.         cout << "Create thread failed!" << endl;  
  31.     }  
  32.     pthread_join(id, NULL);  
  33.     pthread_mutex_destroy(&mutex);  
  34.     return 0;  
  35. }  
  36. //編譯:g++ -o thread testthread.cpp -lpthread  

二、條件變量(cond)

互斥鎖不同,條件變量是用來等待而不是用來上鎖的。條件變量用來自動阻塞一個線程,直到某特殊情況發生為止。通常條件變量和互斥鎖同時使用。條件變量分為兩部分: 條件和變量。條件本身是由互斥量保護的。線程在改變條件狀態前先要鎖住互斥量。條件變量使我們可以睡眠等待某種條件出現。條件變量是利用線程間共享的全局變量進行同步的一種機制,主要包括兩個動作:一個線程等待"條件變量的條件成立"而掛起;另一個線程使"條件成立"(給出條件成立信號)。條件的檢測是在互斥鎖的保護下進行的。如果一個條件為假,一個線程自動阻塞,並釋放等待狀態改變的互斥鎖。如果另一個線程改變了條件,它發信號給關聯的條件變量,喚醒一個或多個等待它的線程,重新獲得互斥鎖,重新評價條件。如果兩進程共享可讀寫的內存,條件變量可以被用來實現這兩進程間的線程同步。

  1. 初始化條件變量。
    靜態態初始化,pthread_cond_t cond = PTHREAD_COND_INITIALIER;
    動態初始化,int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
  2. 等待條件成立。釋放鎖,同時阻塞等待條件變量為真才行。timewait()設置等待時間,仍未signal,返回ETIMEOUT(加鎖保證只有一個線程wait)
    int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
    int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
  3. 激活條件變量。pthread_cond_signal,pthread_cond_broadcast(激活所有等待線程)
    int pthread_cond_signal(pthread_cond_t *cond);
    int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有線程的阻塞
  4. 清除條件變量。無線程等待,否則返回EBUSY
    int pthread_cond_destroy(pthread_cond_t *cond);
[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include "stdlib.h"  
  4. #include "unistd.h"  
  5. pthread_mutex_t mutex;  
  6. pthread_cond_t cond;  
  7. void hander(void *arg)  
  8. {  
  9.     free(arg);  
  10.     (void)pthread_mutex_unlock(&mutex);  
  11. }  
  12. void *thread1(void *arg)  
  13. {  
  14.     pthread_cleanup_push(hander, &mutex);  
  15.     while(1)  
  16.     {  
  17.         printf("thread1 is running\n");  
  18.         pthread_mutex_lock(&mutex);  
  19.         pthread_cond_wait(&cond, &mutex);  
  20.         printf("thread1 applied the condition\n");  
  21.         pthread_mutex_unlock(&mutex);  
  22.         sleep(4);  
  23.     }  
  24.     pthread_cleanup_pop(0);  
  25. }  
  26. void *thread2(void *arg)  
  27. {  
  28.     while(1)  
  29.     {  
  30.         printf("thread2 is running\n");  
  31.         pthread_mutex_lock(&mutex);  
  32.         pthread_cond_wait(&cond, &mutex);  
  33.         printf("thread2 applied the condition\n");  
  34.         pthread_mutex_unlock(&mutex);  
  35.         sleep(1);  
  36.     }  
  37. }  
  38. int main()  
  39. {  
  40.     pthread_t thid1,thid2;  
  41.     printf("condition variable study!\n");  
  42.     pthread_mutex_init(&mutex, NULL);  
  43.     pthread_cond_init(&cond, NULL);  
  44.     pthread_create(&thid1, NULL, thread1, NULL);  
  45.     pthread_create(&thid2, NULL, thread2, NULL);  
  46.     sleep(1);  
  47.     do  
  48.     {  
  49.         pthread_cond_signal(&cond);  
  50.     }while(1);  
  51.     sleep(20);  
  52.     pthread_exit(0);  
  53.     return 0;  
  54. }  
[cpp]  view plain  copy
 
  1. #include <pthread.h>  
  2. #include <unistd.h>  
  3. #include "stdio.h"  
  4. #include "stdlib.h"  
  5. static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;  
  6. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;  
  7. struct node  
  8. {  
  9.     int n_number;  
  10.     struct node *n_next;  
  11. }*head = NULL;  
  12.   
  13. static void cleanup_handler(void *arg)  
  14. {  
  15.     printf("Cleanup handler of second thread./n");  
  16.     free(arg);  
  17.     (void)pthread_mutex_unlock(&mtx);  
  18. }  
  19. static void *thread_func(void *arg)  
  20. {  
  21.     struct node *p = NULL;  
  22.     pthread_cleanup_push(cleanup_handler, p);  
  23.     while (1)  
  24.     {  
  25.         //這個mutex主要是用來保證pthread_cond_wait的並發性  
  26.         pthread_mutex_lock(&mtx);  
  27.         while (head == NULL)  
  28.         {  
  29.             //這個while要特別說明一下,單個pthread_cond_wait功能很完善,為何  
  30.             //這里要有一個while (head == NULL)呢?因為pthread_cond_wait里的線  
  31.             //程可能會被意外喚醒,如果這個時候head != NULL,則不是我們想要的情況。  
  32.             //這個時候,應該讓線程繼續進入pthread_cond_wait  
  33.             // pthread_cond_wait會先解除之前的pthread_mutex_lock鎖定的mtx,  
  34.             //然后阻塞在等待對列里休眠,直到再次被喚醒(大多數情況下是等待的條件成立  
  35.             //而被喚醒,喚醒后,該進程會先鎖定先pthread_mutex_lock(&mtx);,再讀取資源  
  36.             //用這個流程是比較清楚的  
  37.             pthread_cond_wait(&cond, &mtx);  
  38.             p = head;  
  39.             head = head->n_next;  
  40.             printf("Got %d from front of queue/n", p->n_number);  
  41.             free(p);  
  42.         }  
  43.         pthread_mutex_unlock(&mtx); //臨界區數據操作完畢,釋放互斥鎖  
  44.     }  
  45.     pthread_cleanup_pop(0);  
  46.     return 0;  
  47. }  
  48. int main(void)  
  49. {  
  50.     pthread_t tid;  
  51.     int i;  
  52.     struct node *p;  
  53.     //子線程會一直等待資源,類似生產者和消費者,但是這里的消費者可以是多個消費者,而  
  54.     //不僅僅支持普通的單個消費者,這個模型雖然簡單,但是很強大  
  55.     pthread_create(&tid, NULL, thread_func, NULL);  
  56.     sleep(1);  
  57.     for (i = 0; i < 10; i++)  
  58.     {  
  59.         p = (struct node*)malloc(sizeof(struct node));  
  60.         p->n_number = i;  
  61.         pthread_mutex_lock(&mtx); //需要操作head這個臨界資源,先加鎖,  
  62.         p->n_next = head;  
  63.         head = p;  
  64.         pthread_cond_signal(&cond);  
  65.         pthread_mutex_unlock(&mtx); //解鎖  
  66.         sleep(1);  
  67.     }  
  68.     printf("thread 1 wanna end the line.So cancel thread 2./n");  
  69.     //關於pthread_cancel,有一點額外的說明,它是從外部終止子線程,子線程會在最近的取消點,退出  
  70.     //線程,而在我們的代碼里,最近的取消點肯定就是pthread_cond_wait()了。  
  71.     pthread_cancel(tid);  
  72.     pthread_join(tid, NULL);  
  73.     printf("All done -- exiting/n");  
  74.     return 0;  
  75. }  

三、信號量(sem)

如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以"sem_"打頭。線程使用的基本信號量函數有四個。

  1. 信號量初始化。
    int sem_init (sem_t *sem , int pshared, unsigned int value);
    這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux 只支持為0,即表示它是當前進程的局部信號量),然后給它一個初始值VALUE。
  2. 等待信號量。給信號量減1,然后等待直到信號量的值大於0。
    int sem_wait(sem_t *sem);
  3. 釋放信號量。信號量值加1。並通知其他等待線程。
    int sem_post(sem_t *sem);
  4. 銷毀信號量。我們用完信號量后都它進行清理。歸還占有的一切資源。
    int sem_destroy(sem_t *sem);
[cpp]  view plain  copy
 
    1. #include <stdlib.h>  
    2. #include <stdio.h>  
    3. #include <unistd.h>  
    4. #include <pthread.h>  
    5. #include <semaphore.h>  
    6. #include <errno.h>  
    7. #define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}  
    8. typedef struct _PrivInfo  
    9. {  
    10.     sem_t s1;  
    11.     sem_t s2;  
    12.     time_t end_time;  
    13. }PrivInfo;  
    14.   
    15. static void info_init (PrivInfo* thiz);  
    16. static void info_destroy (PrivInfo* thiz);  
    17. static void* pthread_func_1 (PrivInfo* thiz);  
    18. static void* pthread_func_2 (PrivInfo* thiz);  
    19.   
    20. int main (int argc, char** argv)  
    21. {  
    22.     pthread_t pt_1 = 0;  
    23.     pthread_t pt_2 = 0;  
    24.     int ret = 0;  
    25.     PrivInfo* thiz = NULL;  
    26.     thiz = (PrivInfo* )malloc (sizeof (PrivInfo));  
    27.     if (thiz == NULL)  
    28.     {  
    29.         printf ("[%s]: Failed to malloc priv./n");  
    30.         return -1;  
    31.     }  
    32.     info_init (thiz);  
    33.     ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);  
    34.     if (ret != 0)  
    35.     {  
    36.         perror ("pthread_1_create:");  
    37.     }  
    38.     ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);  
    39.     if (ret != 0)  
    40.     {  
    41.         perror ("pthread_2_create:");  
    42.     }  
    43.     pthread_join (pt_1, NULL);  
    44.     pthread_join (pt_2, NULL);  
    45.     info_destroy (thiz);  
    46.     return 0;  
    47. }  
    48. static void info_init (PrivInfo* thiz)  
    49. {  
    50.     return_if_fail (thiz != NULL);  
    51.     thiz->end_time = time(NULL) + 10;  
    52.     sem_init (&thiz->s1, 0, 1);  
    53.     sem_init (&thiz->s2, 0, 0);  
    54.     return;  
    55. }  
    56. static void info_destroy (PrivInfo* thiz)  
    57. {  
    58.     return_if_fail (thiz != NULL);  
    59.     sem_destroy (&thiz->s1);  
    60.     sem_destroy (&thiz->s2);  
    61.     free (thiz);  
    62.     thiz = NULL;  
    63.     return;  
    64. }  
    65. static void* pthread_func_1 (PrivInfo* thiz)  
    66. {  
    67.     return_if_fail(thiz != NULL);  
    68.     while (time(NULL) < thiz->end_time)  
    69.     {  
    70.         sem_wait (&thiz->s2);  
    71.         printf ("pthread1: pthread1 get the lock./n");  
    72.         sem_post (&thiz->s1);  
    73.         printf ("pthread1: pthread1 unlock/n");  
    74.         sleep (1);  
    75.     }  
    76.     return;  
    77. }  
    78. static void* pthread_func_2 (PrivInfo* thiz)  
    79. {  
    80.     return_if_fail (thiz != NULL);  
    81.     while (time (NULL) < thiz->end_time)  
    82.     {  
    83.         sem_wait (&thiz->s1);  
    84.         printf ("pthread2: pthread2 get the unlock./n");  
    85.         sem_post (&thiz->s2);  
    86.         printf ("pthread2: pthread2 unlock./n");  
    87.         sleep (1);  
    88.     }  
    89.     return;  
    90. }  

 

轉:http://blog.csdn.net/zsf8701/article/details/7844316


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM