Linux 互斥锁的实现

/*1.线程访问机制

1.1引言:

任务间的关系有两种:1,间接关系;2,直接关系。

例如1. 任务A对莫个缓冲区进行写操作。任务B从这个缓冲区进行读操作。那么A和B的关系就是直接的关系,那么实现这种直接关系的机制就是同步

2. 任务A要使用打印机,任务b也要使用打印机,那么只有在任务A使用结束的情况下才能使用打印机,所以A和B的关系是间接的关系。那么要实现这种间接的关系的机制是互斥.

在linux中实现线程间的互斥和同步的主要的机制是:信号量和互斥锁

1.2互斥锁

mutex 是一种简单的加锁的方法来控制对共享资源的存取。这个互斥锁只有两种状态,也就是上锁和解锁。

可以把互斥锁看作某种意义上的全局变量。在同一时刻只能有一个线程掌握某个互斥上的锁,拥有上锁状态的线程能够对共享资源进行操作。若其他线程希望上锁一个已经上锁了的互斥锁,则该线程就会挂起,直到上锁的线程释放掉互斥锁为止。可以说,这把互斥锁使得共享资源按序在各个线程中操作。

互斥锁的操作主要包括以下几个步骤。

互斥锁初始化:pthread_mutex_init

互斥锁上锁:pthread_mutex_lock

互斥锁判断上锁:pthread_mutex_trylock

互斥锁接锁:pthread_mutex_unlock

消除互斥锁:pthread_mutex_destroy

其中,互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。这三种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时的是否需要阻塞等待。快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止。递归互斥锁能够成功地返回并且增加调用线程在互斥上加锁的次数,而检错互斥锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误信息。

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)//互斥锁初始化函数

mutexattr =  PTHREAD_MUTEX_INITIALIZER:创建快速互斥锁,快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止

PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP:创建递归函数传入值 Mutexattr 互斥锁,递归互斥锁能够成功地返回并且增加调用线程在互斥上加锁的次数

PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP:创建检错互斥锁,而检错互斥锁则为快速互斥锁的非阻塞版本.

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.c*/
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <pthread.h>
   #include <errno.h>
   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
   int lock_var;
   time_t end_time;
   void pthread1(void *arg);
   void pthread2(void *arg);
   int main(int argc, char *argv[])
  {
    pthread_t id1,id2;
    pthread_t mon_th_id;
    int ret;
    end_time = time(NULL)+10;
   /*互斥锁初始化*/
     pthread_mutex_init(&mutex,NULL);
   /*创建两个线程*/
     ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);
     if(ret!=0)
     perror("pthread cread1");
     ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
     if(ret!=0)
     perror("pthread cread2");
     pthread_join(id1,NULL);                                
     pthread_join(id2,NULL);                                
     exit(0);                                               
   }
                                                         
   void pthread1(void *arg)                               
   {                                                      
    int i;                                                 
    while(time(NULL) < end_time)
    {                          
    /*互斥锁上锁*/                                         
     if(pthread_mutex_lock(&mutex)!=0)
     {                     
      perror("pthread_mutex_lock");                          
     }                                                      
     else                                                   
     printf("pthread1:pthread1 lock the variable\n");       
     for(i=0;i<2;i++){                                      
     sleep(1);                                              
     lock_var++;                                            
     }                                                      
   /*互斥锁接锁*/                                         
     if(pthread_mutex_unlock(&mutex)!=0){                   
     perror("pthread_mutex_unlock");                        
     }                                                      
     else                                                   
     printf("pthread1:pthread1 unlock the variable\n");     
     sleep(1);                                              
    }                                                      
   }                                                      
   void pthread2(void *arg)                               
   {                                                      
    int nolock=0;                                          
    int ret;                                               
    while(time(NULL) < end_time)
    {                          
     /*测试互斥锁*/                                         
     ret=pthread_mutex_trylock(&mutex);                     
     if(ret==EBUSY)                                         
     printf("pthread2:the variable is locked by pthread1\n");
     else
     {  
      if(ret!=0)
      {                                                                                      
        perror("pthread_mutex_trylock");                 
        exit(1);                                         
       }                                                
       else                                             
       printf("pthread2:pthread2 got lock.The variable is%d\n",lock_var);                                 
               /*互斥锁接锁*/                                   
       if(pthread_mutex_unlock(&mutex)!=0)
       {             
        perror("pthread_mutex_unlock");                  
       }                                                
       else                                             
       printf("pthread2:pthread2 unlock the variable\n");
      }                                                
     sleep(3);                                        
     }                                                
    } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/24797.html