mutex 互斥锁线程控制

来源:互联网 发布:强生婴儿知乎 编辑:程序博客网 时间:2024/05/12 19:57

一、引言

    mutex是一种简单的加锁的方法来控制对共享资源的存取。这个互斥所只有两种状态:上锁和解锁。可以把互斥锁看成某种意义上的全局变量。在同一时刻,只能有一个线程掌握某个互斥锁,拥有上锁状态的线程能够对共享资源进行操作。若其它线程希望上锁一个已经上锁的互斥锁,则该线程就会挂起,直到上锁的线程释放该互斥锁为止。可以说,这把锁使得共享资源得以有序在各个线程中操作。

互斥锁主要操作:

1)初始化:pthread_mutex_init

2)上锁:pthread_mutex_lock

3)解锁:pthread_mutex_unlock

4)判断上锁:pthread_mutex_trylock

5)消除互斥锁:pthread_mutex_destroy

=========

     问答

=========

1.可以有多个互斥锁吗?如何存在的?作用区间如何?

答:

二、实例

[cpp] view plaincopy
  1. /*mutex.c*/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <unistd.h>  
  5. #include <pthread.h>  
  6. #include <errno.h>  
  7.   
  8. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  9. int lock_var = 0;  
  10. time_t end_time;  
  11.   
  12. void pthread1(void *arg);  
  13. void pthread2(void *arg);  
  14.   
  15. int main(int argc, char *argv[])  
  16. {  
  17.     pthread_t id1,id2;  
  18.     pthread_t mon_th_id;  
  19.     int ret;  
  20.   
  21.     end_time = time(NULL)+10;  
  22.       
  23.     pthread_mutex_init(&mutex,NULL);  
  24.     /*创建两个线程:pthread1,pthread2*/  
  25.     ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);  
  26.     if(ret!=0)  
  27.         perror("pthread cread1");  
  28.       
  29.     ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);  
  30.     if(ret!=0)  
  31.         perror("pthread cread2");  
  32.       
  33.     pthread_join(id1,NULL);  
  34.     pthread_join(id2,NULL);  
  35.       
  36.     exit(0);  
  37. }  
  38.   
  39. void pthread1(void *arg)  
  40. {  
  41.     int i;  
  42.     while(time(NULL) < end_time)  
  43.     {  
  44. /*互斥所上锁*/  
  45.         if(pthread_mutex_lock(&mutex)!=0)  
  46.         {  
  47.             perror("pthread_mutex_lock");  
  48.         }  
  49.         else  
  50.             printf("pthread1:pthread1 lock the variable\n");  
  51.           
  52.         for(i=0;i<2;i++)          //睡眠两个时间单位  
  53.         {  
  54.             sleep(1);               
  55.             lock_var++;  
  56.         }  
  57.   
  58. /*互斥所解锁*/  
  59.         if(pthread_mutex_unlock(&mutex)!=0)  
  60.         {  
  61.             perror("pthread_mutex_unlock");  
  62.         }  
  63.         else  
  64.             printf("pthread1:pthread1 unlock the variable\n");  
  65.           
  66.         sleep(1);           //睡眠1个时间单位  
  67.     }  
  68. }  
  69.   
  70. void pthread2(void *arg)  
  71. {  
  72.     int nolock=0;  
  73.     int ret;  
  74.       
  75.     while(time(NULL) < end_time)  
  76.     {  
  77. /*测试锁状态*/  
  78.         ret=pthread_mutex_trylock(&mutex);  
  79.         if(ret==EBUSY)                         //若为忙,表示被其它线程占用  
  80.             printf("pthread2:the variable is locked by pthread1\n");  
  81.         else  
  82.         {  
  83.             if(ret!=0)  
  84.             {  
  85.                 perror("pthread_mutex_trylock");  
  86.                 exit(1);  
  87.             }  
  88.             else  
  89.                 printf("pthread2:pthread2 got lock.The variable is %d\n",lock_var);  
  90. /*解锁*/  
  91.             if(pthread_mutex_unlock(&mutex)!=0)  
  92.             {  
  93.                 perror("pthread_mutex_unlock");  
  94.             }  
  95.              else  
  96.                 printf("pthread2:pthread2 unlock the variable\n");  
  97.         }  
  98.         sleep(3);  //睡眠3个时间单位  
  99.     }  
  100. }  

运行结果如下:

[cpp] view plaincopy
  1. [root@localhost net]# ./mutex  
  2. pthread1:pthread1 lock the variable  
  3. pthread2:the variable is locked by pthread1  
  4. pthread1:pthread1 unlock the variable  
  5. pthread2:pthread2 got lock.The variable is 2  
  6. pthread2:pthread2 unlock the variable  
  7. pthread1:pthread1 lock the variable  
  8. pthread1:pthread1 unlock the variable  
  9. pthread2:pthread2 got lock.The variable is 4  
  10. pthread2:pthread2 unlock the variable  
  11. pthread1:pthread1 lock the variable  
  12. pthread1:pthread1 unlock the variable  
  13. pthread2:pthread2 got lock.The variable is 6  
  14. pthread2:pthread2 unlock the variable  
  15. pthread1:pthread1 lock the variable  
  16. pthread1:pthread1 unlock the variable 

原创粉丝点击