线程同步与互斥——互斥锁

浏览博客时发现一篇博客写的不错就转载了一下,原博客网址:http://blog.csdn.net/tennysonsky/article/details/46494077

为何须要互斥锁?

在多任务操做系统中,同时运行的多个任务可能都须要使用同一种资源。这个过程有点相似于,公司部门里,我在使用着打印机打印东西的同时(尚未打印完),别人恰好也在此刻使用打印机打印东西,若是不作任何处理的话,打印出来的东西确定是错乱的。ide


下面咱们用程序模拟一下这个过程,线程一须要打印“ hello ”,线程二须要打印“ world ”,不加任何处理的话,打印出来的内容会错乱:函数

[cpp] view plain copyurl

  1. #include <stdio.h>  spa

  2. #include <pthread.h>  操作系统

  3. #include <unistd.h>  .net

  4.   

  5. // 打印机  线程

  6. void printer(char *str)  orm

  7. {  blog

  8.     while(*str!='\0')  ip

  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 错误码


互斥锁应用实例

咱们经过互斥锁完善上面的例子,示例代码以下:

[cpp] view plain copy

  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. }  



运行结果以下:


相关文章
相关标签/搜索