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

来源:互联网 发布:阿里云 ss 编辑:程序博客网 时间:2024/05/17 23:15

  今天我们来分享一下,线程同步与互斥——互斥锁的实现。

  多个线程同时访问共享数据时可能会产生冲突,造成程序运行结果不是我们所预期的结果。

  不产生冲突的多线程访问情况,代码和截图如下:

 

 

  产生冲突的多线程访问情况,代码和截图如下:

 

 

注:每运行一次,结果都可能会不同。

   由于多线程访问共享数据时可能会产生冲突,不能保证线程之间的同步与互斥。所以为了保证多线程访问共享数据时能够实现同步与互斥,所以我们可以使用线程的互斥锁。

   当线程进入临界区时,该线程加锁,以此来确保此时占有临界资源,以此来实现互斥。当线程完成在临界区的一系列操作后,该线程释放锁,以此来确保其他线程可以占有临界资源,以此来实现同步。

   互斥锁实现线程同步互斥的代码截图和运行结果如下:

 

 

 

互斥锁的源代码如下:

mypthread.c

[cpp] view plain copy
print?
  1. <span style=“font-family:SimSun;font-size:18px;”>#include<stdio.h>  
  2.    #include<pthread.h>  
  3.      
  4.    //pthread_mutex_t lockp = PTHREAD_MUTEX_INITIALIZER;//可用全局锁  
  5.      
  6.    int code = 0;  
  7.      
  8.    void* thread_fun(void* arg){  
  9.        pthread_mutex_t* lockp = (pthread_mutex_t*)arg;  
  10.       int i=0;  
  11.       while(i<500){  
  12.           pthread_mutex_lock(lockp);  
  13.           int val = code;  
  14.           printf(” pthread pid:%d, tid:%u, code:%d\n”,getpid(), pthread_self(), code);  
  15.           usleep(30);  
  16.     
  17.           code = val+1;  
  18.           i++;  
  19.           pthread_mutex_unlock(lockp);  
  20.       }  
  21.       return (void*)1;  
  22.   }  
  23.     
  24.   int main(){  
  25.       pthread_t tid1;  
  26.       pthread_t tid2;  
  27.     
  28.       pthread_mutex_t lockp;  
  29.       pthread_mutex_init(&lockp, 0);  
  30.     
  31.       if(pthread_create(&tid1, NULL, thread_fun, &lockp)<0){  
  32.           perror(”pthread_creat”);  
  33.           return -1;  
  34.       }  
  35.       if(pthread_create(&tid2, NULL, thread_fun, &lockp)<0){  
  36.           perror(”pthread_creat”);  
  37.          return -2;  
  38.       }  
  39.     
  40.       pthread_join(tid1, NULL);  
  41.       pthread_join(tid2, NULL);  
  42.       pthread_mutex_destroy(&lockp);  
  43.       return 0;  
  44.   }</span>  
#include<stdio.h>   #include<pthread.h>   //pthread_mutex_t lockp = PTHREAD_MUTEX_INITIALIZER;//可用全局锁   int code = 0;   void* thread_fun(void* arg){       pthread_mutex_t* lockp = (pthread_mutex_t*)arg;      int i=0;      while(i<500){          pthread_mutex_lock(lockp);          int val = code;          printf(" pthread pid:%d, tid:%u, code:%d\n",getpid(), pthread_self(), code);          usleep(30);          code = val+1;          i++;          pthread_mutex_unlock(lockp);      }      return (void*)1;  }  int main(){      pthread_t tid1;      pthread_t tid2;      pthread_mutex_t lockp;      pthread_mutex_init(&lockp, 0);      if(pthread_create(&tid1, NULL, thread_fun, &lockp)<0){          perror("pthread_creat");          return -1;      }      if(pthread_create(&tid2, NULL, thread_fun, &lockp)<0){          perror("pthread_creat");         return -2;      }      pthread_join(tid1, NULL);      pthread_join(tid2, NULL);      pthread_mutex_destroy(&lockp);      return 0;  }

Makefile  

[cpp] view plain copy
print?
  1. <span style=“font-family:SimSun;font-size:18px;”>mythread:mythread.c  
  2.       gcc -o @&nbsp;^ -lpthread  
  3.   .PHONY:clean  
  4.   clean:  
  5.       rm -f mythread</span>  
mythread:mythread.c      gcc -o $@ $^ -lpthread  .PHONY:clean  clean:      rm -f mythread

分享如上,愿大家天天有进步!

 

原创粉丝点击