Linux操作系统多线程同步互斥Mutex的使用

来源:互联网 发布:河南民生频道网络电视 编辑:程序博客网 时间:2024/05/21 18:11

1. 初始化

在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化:

对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init。

对于动态分配的互斥量, 在申请内存(malloc)之后, 通过pthread_mutex_init进行初始化, 并且在释放内存(free)前需要调用pthread_mutex_destroy。

头文件:#include <pthread.h>

原型:

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restric attr);int pthread_mutex_destroy(pthread_mutex_t *mutex); 


返回值:成功则返回0, 出错则返回错误编号。

说明:如果使用默认的属性初始化互斥量, 只需把attr设为NULL. 其他值在以后讲解。

2. 互斥操作

对共享资源的访问, 要对互斥量进行加锁, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被解锁. 在完成了对共享资源的访问后, 要对互斥量进行解锁。

首先说一下加锁函数:

头文件: #include <pthread.h>

原型:

int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex); 


返回值: 成功则返回0, 出错则返回错误编号。

说明: 具体说一下trylock函数,这个函数是非阻塞调用模式,也就是说,如果互斥量没被锁住,trylock函数将把互斥量加锁,并获得对共享资源的访问权限;如果互斥量被锁住了, trylock函数将不会阻塞等待而直接返回EBUSY,表示共享资源处于忙状态。

 

再说一下解锁函数:

头文件: #include<pthread.h>

原型:

int pthread_mutex_unlock(pthread_mutex_t *mutex); 

返回值: 成功则返回0,出错则返回错误编号。

3. 死锁

死锁主要发生在有多个依赖锁存在时,会在一个线程试图以与另一个线程相反顺序锁住互斥量时发生。如何避免死锁是使用互斥量应该格外注意的东西。

总体来讲, 有几个不成文的基本原则:

对共享资源操作前一定要获得锁。

完成操作以后一定要释放锁。

尽量短时间地占用锁。

如果有多锁,如获得顺序是ABC连环扣,释放顺序也应该是ABC。

线程错误返回时应该释放它所获得的锁

4.代码样例

#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);//定义线程函数1void pthread2(void *arg);//定义线程函数2int 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);//对互斥量mutex进行初始化,使用默认的属性初始化互斥量(即第二个变量为NULL)ret=pthread_create(&id1,NULL,(void *)pthread1, NULL);//创建线程1if(ret!=0)perror("pthread cread1");ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);//创建线程2if(ret!=0)perror("pthread cread2");pthread_join(id1,NULL);//等待一个线程1的结束pthread_join(id2,NULL);//等待一个线程2的结束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(2);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(1);}}

注:因为pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程等其他线程操作时,需要链接该库。即在编译中要加 -lpthread参数。

运行结果:


pthread2:pthread2 got lock.The variable is 0pthread2:pthread2 unlock the variablepthread1:pthread1 lock the variablepthread2:the variable is locked by pthread1pthread2:the variable is locked by pthread1pthread2:the variable is locked by pthread1pthread1:pthread1 unlock the variablepthread2:pthread2 got lock.The variable is 2pthread2:pthread2 unlock the variablepthread1:pthread1 lock the variablepthread2:the variable is locked by pthread1pthread2:the variable is locked by pthread1pthread2:the variable is locked by pthread1pthread2:the variable is locked by pthread1pthread1:pthread1 unlock the variablepthread2:pthread2 got lock.The variable is 4pthread2:pthread2 unlock the variable


原文地址:

Linux操作系统多线程同步Mutex详细介绍