互斥锁

wuyuwww 2012-04-17 11:35:52
为什么这个代码执行的使只输入了一部分,后面的执行就很卡
为啥啊
代码如下:
#include<pthread.h>
#include<stdio.h>
#include<errno.h>

int i=0;

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void thread1()
{
printf("come in thread1\n");
int ret;
while(1)
{
ret=pthread_mutex_trylock(&mutex);

if(ret!=EBUSY)
{
pthread_mutex_lock(&mutex);

printf("this is thread1:%d\n",i);
i++;
pthread_mutex_unlock(&mutex);

}
sleep(1);
}
}

void thread2()
{
printf("come in thread2\n");
int ret;
while(1)
{
ret=pthread_mutex_trylock(&mutex);
if(ret!=EBUSY)
{
pthread_mutex_lock(&mutex);
printf("this is thread2:%d\n",i);
i++;
pthread_mutex_unlock(&mutex);
}
sleep(1);
}
}

int main()
{
pthread_t t1,t2;
printf("main start\n");
pthread_mutex_init(&mutex,NULL);
printf("this is in main and after mutex init\n");
pthread_create(&t1,NULL,(void *)thread1,NULL);

printf("this is in main and at middle of create thread1 and thread2\n");
pthread_create(&t2,NULL,(void *)thread2,NULL);
printf("this is in main and after create thred1 and thread2\n");

pthread_join(t1,NULL);
printf("this is in main and after thread1 join\n");
pthread_join(t2,NULL);
printf("this is after thread2 join\n");

pthread_mutex_destroy(&mutex);
return 0;
}


执行后输出:
main start
this is in main and after mutex init
this is in main and at middle of create thread1 and thread2
this is in main and after create thred1 and thread2
come in thread2
come in thread1

然后就一直卡死在里面
...全文
287 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_cxf 2012-04-17
  • 打赏
  • 举报
回复
主循环destroy(mutex)要加while(1)把
不然你mutex都destroy了,还怎么用啊
enjolras 2012-04-17
  • 打赏
  • 举报
回复
pthread_mutex_trylock返回0的话表示已经锁住了。。

你pthread_mutex_lock再锁肯定就没法再获得锁了。
这种情况改成这样就行了:
pthread_mutex_lock(&mutex);
printf("this is thread1:%d\n",i);
i++;
pthread_mutex_unlock(&mutex);
wuyuwww 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
楼主的目的是在主线程中创建出两个线程,两个线程通过一个死循环交替的打印全局变量i的值,然后主调线程阻塞。
这样写是达不到你的目的的,分析如下:
void thread1()
{
printf("come in thread1\n");
int ret;
while(1)
{
ret=pthread_mutex_trylock(&mutex); //尝试给……
[/Quote]

谢谢你啊 我修改了一下 就出结果了 果然是死锁
我删除了一个锁

帅哥 以后还请多多指教啊
wuyuwww 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
主循环destroy(mutex)要加while(1)把
不然你mutex都destroy了,还怎么用啊
[/Quote]

前面不是有pthread_join 吗 这个不是挂起主线程吗 你的接受貌似不对
惬意 2012-04-17
  • 打赏
  • 举报
回复
楼主的目的是在主线程中创建出两个线程,两个线程通过一个死循环交替的打印全局变量i的值,然后主调线程阻塞。
这样写是达不到你的目的的,分析如下:
void thread1()
{
printf("come in thread1\n");
int ret;
while(1)
{
ret=pthread_mutex_trylock(&mutex); //尝试给互斥变量加锁,问题:如果这里加锁成功返回0

if(ret!=EBUSY)
{
pthread_mutex_lock(&mutex); //会执行到这里,也就是说再次加锁,这样也就产生了死锁(对一把锁加锁两次)

printf("this is thread1:%d\n",i);
i++;
pthread_mutex_unlock(&mutex); //就算你的互斥两属性是PTHREAD_MUTEX_RECURSIVE,那么你加锁两次释放锁也应该需要两次,锁才会是解锁状态,否则其他的线程是无法对该互斥变量加锁的

}
sleep(1);
}
}

正在充电中 2012-04-17
  • 打赏
  • 举报
回复
1楼正解

64,635

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧