linux__mutex互斥锁___操作

[html]  view plain copy
  1. mutex是死等锁,即pthread_mutex_lock(&mutex);这条语句在执行的后,做两步,1,先判断当前是否可以锁,如别的线程在锁住,这里就一直等待直到别的线程解锁为止。1,判断完后,开始锁住,防止别的线程使用。  
  2. 如果不想在这里死等,可以采用pthread_mutex_trylock(&mutex);然后判断if(ret==EBUSY){被别的线程锁住,这里做相应的无法锁住的事情,如返回}  
[html]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <pthread.h>  
  5. #include <errno.h>  
  6.   
  7. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  8. int lock_var;  
  9. time_t end_time;  
  10. int sum;  
  11.   
  12. void pthread1(void *arg);  
  13. void pthread2(void *arg);  
  14. int main(int argc, char *argv[])  
  15. {  
  16.       
  17.         pthread_t id1,id2;  
  18.         pthread_t mon_th_id;  
  19.         int ret;  
  20.         sum=10;  
  21.           
  22.         end_time = time(NULL)+10;  
  23.           
  24.         pthread_mutex_init(&mutex,NULL);  
  25.         ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);  
  26.           
  27.         if(ret!=0)  
  28.         perror("pthread cread1");  
  29.           
  30.         ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);  
  31.         if(ret!=0)  
  32.         perror("pthread cread2");  
  33.           
  34.       
  35.           
  36.         pthread_join(id1,NULL);  
  37.         pthread_join(id2,NULL);  
  38.         exit(0);  
  39. }  
  40.   
  41. void pthread1(void *arg)  
  42. {  
  43.         int i;  
  44.         while(time(NULL) < end_time)  
  45.         {  
  46.                     pthread_mutex_lock(&mutex); //lock  
  47.                           
  48.                     printf("pthread1: lock the variable\n");  
  49.                       
  50.                     for(i=0;i<2;i++)  
  51.                     {  
  52.                         sleep(2);  
  53.                       lock_var++;  
  54.                     }  
  55.                  pthread_mutex_unlock(&mutex); //unlock  
  56.                 printf("pthread1:unlock the variable\n");  
  57.                     sleep(1);  
  58.         }  
  59. }  
  60.   
  61. void pthread2(void *arg)  
  62. {  
  63.             int nolock=0;  
  64.             int ret;  
  65.             while(time(NULL) < end_time)  
  66.             {  
  67.                           
  68.                         ret=pthread_mutex_lock(&mutex);  
  69.                         printf("pthread2:got lock.\n");  
  70.                           
  71.                         pthread_mutex_unlock(&mutex);//unlock  
  72.                           
  73.                         printf("pthread2:unlock the variable\n");  
  74.                         sleep(1);  
  75.             }  
  76. }  



转自:http://blog.csdn.net/wtz1985/article/details/3819052

一个进程中的多个线程是共享同一段资源的,由于线程对资源的竞争引出了锁。其中mutex是一种简单的加锁方法,这个互斥锁只有两种状态,那就是上锁和解锁,可以把互斥锁看作是某种意义上的全局变量。在某一时刻,只能有一个线程取得这个互斥上的锁,拥有上锁状态的线程可以对共享资源进行操作,而其他线程在该线程未解锁之前,够会被挂起,直到上锁的线程解开锁。可以这么说,互斥锁使得共享资源按序的在各个线程上操作。

互斥锁的操作主要包括互斥锁初始化、上锁、判断上锁、解锁、摧毁互斥锁。其中互斥锁可以分为快速互斥锁、递归互斥锁这检错互斥锁。这三种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时是否需要等待挂起。快速锁是指调用线程会阻塞直到线程锁得到解锁为止。递归锁能够成功地返回并且增加调用线程在互斥上的加锁次数,比如一个链表在进行插入的操作时,可以进行查找的操作。检错锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误的信息。

1、函数简义。

(1)pthread_mutex_init

头文件:                  <pthread.h>

函数原型:               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:互斥锁。

函数返回值:            同上。

2、互斥锁实现。

[cpp]  view plain copy
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <pthread.h>    
  4. #include <errno.h>    
  5. #include <unistd.h>    
  6.     
  7. #define  return_if_fail(p)  \  
  8.     if((p) == 0) \  
  9.         { \  
  10.             printf("[%s]:func error!/n",__func__); \  
  11.             return; \  
  12.         }  \  
  13.      
  14. typedef struct _PrivInfo    
  15. {    
  16.    pthread_mutex_t mutex;    
  17.    int lock_var;    
  18.    time_t end_time;    
  19. }PrivInfo;    
  20.     
  21. static void info_init (PrivInfo* thiz);    
  22. static void* pthread_func_1 (PrivInfo* thiz);    
  23. static void* pthread_func_2 (PrivInfo* thiz);    
  24.     
  25. int main (int argc, char** argv)    
  26. {    
  27.    pthread_t pt_1 = 0;    
  28.    pthread_t pt_2 = 0;    
  29.    int ret = 0;    
  30.    PrivInfo* thiz = NULL;    
  31.     
  32.    thiz = (PrivInfo*)malloc (sizeof (PrivInfo));    
  33.    if (thiz == NULL)    
  34.    {    
  35.       return -1;    
  36.    }    
  37.     
  38.    info_init(thiz);    
  39.     
  40.    ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);    
  41.    if (ret != 0)    
  42.    {    
  43.      perror ("pthread_1_create:");    
  44.    }    
  45.        
  46.    ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);    
  47.    {    
  48.      perror ("pthread_2_create:");    
  49.    }    
  50.     
  51.    pthread_join (pt_1, NULL);    
  52.    pthread_join (pt_2, NULL);    
  53.     
  54.    pthread_mutex_destroy (&thiz->mutex);    
  55.        
  56.    free (thiz);    
  57.    thiz = NULL;    
  58.     
  59.    return 0;    
  60. }    
  61.     
  62. static void info_init (PrivInfo* thiz)    
  63. {    
  64.    return_if_fail (thiz != NULL);    
  65.     
  66.    thiz->lock_var = 0;    
  67.    thiz->end_time = time (NULL) + 10;    
  68.     
  69.    pthread_mutex_init (&thiz->mutex, NULL);    
  70.     
  71.    return;    
  72. }    
  73.     
  74. static void* pthread_func_1 (PrivInfo* thiz)    
  75. {    
  76.    return_if_fail (thiz != NULL);    
  77.     
  78.    int i = 0;    
  79.    int ret = 0;    
  80.     
  81.    while (time (NULL) < thiz->end_time)    
  82.    {    
  83.      ret  = pthread_mutex_lock (&thiz->mutex);    
  84.      if (ret != 0)    
  85.      {    
  86.          perror ("[%s]pthread_mutex_lock");    
  87.      }    
  88.      else    
  89.      {    
  90.        printf ("pthread1:pthread1 lock the variable!\n");    
  91.      }    
  92.     
  93.      for (i = 0; i < 2; i++)    
  94.      {    
  95.         sleep (1);    
  96.         thiz->lock_var ++;    
  97.      }    
  98.     
  99.      ret = pthread_mutex_unlock (&thiz->mutex);    
  100.      if (ret != 0)    
  101.      {    
  102.         perror ("[%s]pthread_mutex_unlock");    
  103.      }    
  104.      else    
  105.      {    
  106.         printf ("pthread1: pthread1 unlock the variable.\n");    
  107.      }         
  108.    }    
  109.     
  110.    return;    
  111. }    
  112.     
  113. static void* pthread_func_2 (PrivInfo* thiz)    
  114. {    
  115.    return_if_fail (thiz != NULL);    
  116.     
  117.    int ret = 0;    
  118.        
  119.    while (time (NULL) < thiz->end_time)    
  120.    {    
  121.       ret = pthread_mutex_trylock (&thiz->mutex);    
  122.       if (ret == EBUSY)    
  123.       {    
  124.          printf ("pthread2:the variable is locked by thread1.\n");    
  125.       }    
  126.       else    
  127.       {    
  128.          if (ret != 0)    
  129.          {    
  130.              perror ("[%s]pthread2_mutex_trylock");     
  131.          }    
  132.          else    
  133.          {    
  134.             printf ("pthread2: pthread2 lock the variable.\n");    
  135.          }             
  136.           
  137.     
  138.          ret = pthread_mutex_unlock (&thiz->mutex);    
  139.          if (ret != 0)    
  140.          {    
  141.            perror ("[%s]pthread_mutex_lock");    
  142.          }    
  143.       }  
  144.        sleep (3);      
  145.     }    
  146. }    


在上例的中,是对变量lock_var的读写进行加锁,线程一是对其进行写,在写的时候,线程二不能对其进行读。直到线程一解锁,线程二得到互斥锁,才能进行读。同样,在线程二进行读的时候,线程一不能进行写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值