首页 > 技术文章 > 线程同步与相互排斥:相互排斥锁

mthoutai 2017-08-02 20:50 原文

为什么须要相互排斥锁?

在多任务操作系统中,同一时候执行的多个任务可能都须要使用同一种资源。这个过程有点类似于,公司部门里。我在使用着打印机打印东西的同一时候(还没有打印完)。别人刚好也在此刻使用打印机打印东西,假设不做不论什么处理的话,打印出来的东西肯定是错乱的。


以下我们用程序模拟一下这个过程。线程一须要打印“ hello ”,线程二须要打印“ world ”,不加不论什么处理的话。打印出来的内容会错乱:

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <unistd.h>  
  4.   
  5. // 打印机  
  6. void printer(char *str)  
  7. {  
  8.     while(*str!='\0')  
  9.     {  
  10.         putchar(*str);    
  11.         fflush(stdout);  
  12.         str++;  
  13.         sleep(1);  
  14.     }  
  15.     printf("\n");   
  16. }  
  17.   
  18. // 线程一  
  19. void *thread_fun_1(void *arg)  
  20. {  
  21.     char *str = "hello";  
  22.     printer(str); //打印  
  23. }  
  24.   
  25. // 线程二  
  26. void *thread_fun_2(void *arg)  
  27. {  
  28.     char *str = "world";  
  29.     printer(str); //打印  
  30. }  
  31.   
  32. int main(void)  
  33. {  
  34.     pthread_t tid1, tid2;  
  35.       
  36.     // 创建 2 个线程  
  37.     pthread_create(&tid1, NULL, thread_fun_1, NULL);  
  38.     pthread_create(&tid2, NULL, thread_fun_2, NULL);  
  39.   
  40.     // 等待线程结束,回收其资源  
  41.     pthread_join(tid1, NULL);  
  42.     pthread_join(tid2, NULL);   
  43.       
  44.     return 0;  
  45. }  

执行结果例如以下:



实际上,打印机是有做处理的,我在打印着的时候别人是不同意打印的。仅仅有等我打印结束后别人才同意打印。这个过程有点类似于,把打印机放在一个房间里,给这个房间安把锁,这个锁默认是打开的。当 A 须要打印时,他先过来检查这把锁有没有锁着。没有的话就进去,同一时候上锁在房间里打印。而在这时,刚好 B 也须要打印。B 相同先检查锁,发现锁是锁住的。他就在门外等着。而当 A 打印结束后,他会开锁出来,这时候 B 才进去上锁打印。


而在线程里也有这么一把锁——相互排斥锁(mutex),相互排斥锁是一种简单的加锁的方法来控制对共享资源的訪问,相互排斥锁仅仅有两种状态,即上锁( lock )和解锁( unlock )。


相互排斥锁的操作流程例如以下:

1)在訪问共享资源后临界区域前,对相互排斥锁进行加锁。

2)在訪问完毕后释放相互排斥锁导上的锁。

3)对相互排斥锁进行加锁后,不论什么其它试图再次对相互排斥锁加锁的线程将会被堵塞,直到锁被释放。


相互排斥锁的数据类型是: pthread_mutex_t


相互排斥锁基本操作


下面函数须要的头文件:

#include <pthread.h>


1)初始化相互排斥锁

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);

功能:

初始化一个相互排斥锁。


參数:

mutex:相互排斥锁地址。类型是 pthread_mutex_t 。
attr:设置相互排斥量的属性,通常可採用默认属性。就可以将 attr 设为 NULL。


能够使用宏 PTHREAD_MUTEX_INITIALIZER 静态初始化相互排斥锁。比方:
pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
这样的方法等价于使用 NULL 指定的 attr 參数调用 pthread_mutex_init() 来完毕动态初始化,不同之处在于 PTHREAD_MUTEX_INITIALIZER 宏不进行错误检查。

返回值:

成功:0。成功申请的锁默认是打开的。

失败:非 0 错误码


2)上锁

int pthread_mutex_lock(pthread_mutex_t *mutex);

功能:

对相互排斥锁上锁。若相互排斥锁已经上锁,则调用者一直堵塞,直到相互排斥锁解锁后再上锁。

參数:

mutex:相互排斥锁地址。


返回值:

成功:0

失败:非 0 错误码


int pthread_mutex_trylock(pthread_mutex_t *mutex);

调用该函数时,若相互排斥锁未加锁,则上锁。返回 0;若相互排斥锁已加锁,则函数直接返回失败。即 EBUSY。


3)解锁

int pthread_mutex_unlock(pthread_mutex_t * mutex);

功能:

对指定的相互排斥锁解锁。

參数:

mutex:相互排斥锁地址。

返回值:

成功:0

失败:非 0 错误码


4)销毁相互排斥锁

int pthread_mutex_destroy(pthread_mutex_t *mutex);

功能:

销毁指定的一个相互排斥锁。相互排斥锁在使用完成后,必需要对相互排斥锁进行销毁。以释放资源。

參数:

mutex:相互排斥锁地址。

返回值:

成功:0

失败:非 0 错误码


相互排斥锁应用实例

我们通过相互排斥锁完好上面的样例。演示样例代码例如以下:

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #include <unistd.h>  
  4.   
  5. pthread_mutex_t mutex; //相互排斥锁  
  6.   
  7. // 打印机  
  8. void printer(char *str)  
  9. {  
  10.     pthread_mutex_lock(&mutex); //上锁  
  11.     while(*str!='\0')  
  12.     {  
  13.         putchar(*str);    
  14.         fflush(stdout);  
  15.         str++;  
  16.         sleep(1);  
  17.     }  
  18.     printf("\n");   
  19.     pthread_mutex_unlock(&mutex); //解锁  
  20. }  
  21.   
  22. // 线程一  
  23. void *thread_fun_1(void *arg)  
  24. {  
  25.     char *str = "hello";  
  26.     printer(str); //打印  
  27. }  
  28.   
  29. // 线程二  
  30. void *thread_fun_2(void *arg)  
  31. {  
  32.     char *str = "world";  
  33.     printer(str); //打印  
  34. }  
  35.   
  36. int main(void)  
  37. {  
  38.     pthread_t tid1, tid2;  
  39.       
  40.     pthread_mutex_init(&mutex, NULL); //初始化相互排斥锁  
  41.       
  42.     // 创建 2 个线程  
  43.     pthread_create(&tid1, NULL, thread_fun_1, NULL);  
  44.     pthread_create(&tid2, NULL, thread_fun_2, NULL);  
  45.   
  46.     // 等待线程结束。回收其资源  
  47.     pthread_join(tid1, NULL);  
  48.     pthread_join(tid2, NULL);   
  49.       
  50.     pthread_mutex_destroy(&mutex); //销毁相互排斥锁  
  51.       
  52.     return 0;  
  53. }  


执行结果例如以下:


本教程演示样例代码下载请点此处。

推荐阅读