良许Linux教程网 干货合集 Linux多线程同步互斥量Mutex详解

Linux多线程同步互斥量Mutex详解

Linux系统是一种支持多任务并发执行的操作系统,它可以同时运行多个进程,从而提高系统的利用率和效率。但是,如果一个进程中有多个线程,而这些线程需要共享一些数据或资源,就可能出现数据不一致或资源竞争的问题,导致系统的错误或异常。为了解决这个问题,就需要使用一些同步机制,例如信号量、条件变量、互斥量等。其中,互斥量是一种比较简单而有效的同步机制,它可以让一个线程在访问共享数据或资源时,锁定它们,防止其他线程同时访问,从而保证线程安全。本文将详解Linux多线程同步互斥量Mutex的方法,包括互斥量的初始化、加锁、解锁和销毁等方面。

1. 初始化

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

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

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

原型:

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. 互斥操作

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

img说一下加锁函数:

头文件:

原型:

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

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

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

再说一下解所函数:

头文件:

原型: int pthread_mutex_unlock(pthread_mutex_t *mutex);

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

\3. 死锁:

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

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

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

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

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

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

示例:

#include 
#include 
#include 
#include 
#include 

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int lock_var;
time_t end_time;
int sum;

void pthread1(void *arg);
void pthread2(void *arg);
void pthread3(void *arg);

int main(int argc, char *argv[])
{
pthread_t id1,id2,id3;
pthread_t mon_th_id;
int ret;
sum=10;

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");

ret=pthread_create(&id3,NULL,(void *)pthread3, NULL);
if(ret!=0)
perror("pthread cread3");

pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
exit(0);
}

void pthread1(void *arg)
{
int i;
while(time(NULL) if(pthread_mutex_lock(&mutex)!=0) //lock
{
perror("pthread_mutex_lock");
}
else
printf("pthread1:pthread1 lock the variablen");
for(i=0;iif(pthread_mutex_unlock(&mutex)!=0) //unlock
{
perror("pthread_mutex_unlock");
}
else
printf("pthread1:pthread1 unlock the variablen");
sleep(1);
}
}

void pthread2(void *arg)
{
int nolock=0;
int ret;
while(time(NULL) if(ret==EBUSY)
printf("pthread2:the variable is locked by pthread1n");
else{
if(ret!=0)
{
perror("pthread_mutex_trylock");
exit(1);
}
else
printf("pthread2:pthread2 got lock.The variable is %dn",lock_var);
if(pthread_mutex_unlock(&mutex)!=0)//unlock
{
perror("pthread_mutex_unlock");
}
else
printf("pthread2:pthread2 unlock the variablen");
}
sleep(1);
}
}



void pthread3(void *arg)
{/*
int nolock=0;
int ret;
while(time(NULL) if(ret==EBUSY)
printf("pthread3:the variable is locked by pthread1 or 2n");
else
{
if(ret!=0)
{
perror("pthread_mutex_trylock");
exit(1);
}
else
printf("pthread3:pthread3 got lock.The variable is %dn",lock_var);
if(pthread_mutex_unlock(&mutex)!=0)
{
perror("pthread_mutex_unlock");
}
else
printf("pthread3:pthread2 unlock the variablen");
}
sleep(3);
}*/
}

本文详解了Linux多线程同步互斥量Mutex的方法,包括互斥量的初始化、加锁、解锁和销毁等方面。通过了解和掌握这些知识,我们可以更好地使用互斥量来实现多线程之间的同步,提高系统的稳定性和效率。

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部