免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3377 | 回复: 12
打印 上一主题 下一主题

大伙帮忙看下一个关于mutex的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-01 13:02 |只看该作者 |倒序浏览
有人帮我看下这个问题,为什么mutex 会被锁两次:
代码就是等待输入一个q返回,源代码如下,红色的两行结果
好奇怪,谢谢大牛了
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

static pthread_t thread;
static pthread_cond_t cond;
static pthread_mutex_t mutex;
static int flag = 1;

void * thr_fn(void * arg)
{
  int ret =0 ;
        struct timeval now;
  struct timespec outtime;
  printf("in function lock begin!\n");
        ret = pthread_mutex_lock(&mutex);
  printf("in function lock end! ret=%d\n",ret);
  while (flag) {
    printf("*****\n");
    gettimeofday(&now, NULL);
    outtime.tv_sec = now.tv_sec + 5;
    outtime.tv_nsec = now.tv_usec * 1000;
    pthread_cond_timedwait(&cond, &mutex, &outtime);
  }
  printf("in function unlock begin! ret=%d\n",ret);
  ret=pthread_mutex_unlock(&mutex);
  printf("in function unlock end! ret=%d\n",ret);
  printf("cond thread exit\n");
}

int main(void)
{
  int ret = 0;
  pthread_mutex_init(&mutex, NULL);
  pthread_cond_init(&cond, NULL);
  if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {
    printf("error when create pthread,%d\n", errno);
    return 1;
  }
  char c ;
  while ((c = getchar()) != 'q');
  printf("Now terminate the thread!\n");

  printf("main lock begin!i ret=%d\n",ret);
ret= pthread_mutex_lock(&mutex);
  printf("main lock end! ret=%d\n",ret);
  flag = 0;
  pthread_cond_signal(&cond);
  printf("main unlock begin!\n");
ret =  pthread_mutex_unlock(&mutex);
  printf("main unlock end! ret=%d\n",ret);
  printf("Wait for thread to exit\n");
  pthread_join(thread, NULL);
  printf("Bye\n");
  return 0;
}

in function lock begin!
in function lock end! ret=0
*****
*****
q
Now terminate the thread!
main lock begin!i ret=0
main lock end! ret=0
main unlock begin!
main unlock end! ret=0
Wait for thread to exit
in function unlock begin! ret=0
in function unlock end! ret=0
cond thread exit
Bye

论坛徽章:
0
2 [报告]
发表于 2014-12-01 16:18 |只看该作者
你代码就那么写了,lock 2次才正常啊。
要不然你期望lock几次?

论坛徽章:
0
3 [报告]
发表于 2014-12-01 16:26 |只看该作者
谢谢你的回复,是啊,main 线程和 子线程 lock调用都成功返回了,但它们都操作的同一个mutex,问题是两个thread锁同一个mutex为什么会都成功?回复 2# GreatChinaWolf


   

论坛徽章:
0
4 [报告]
发表于 2014-12-02 08:54 |只看该作者
玩线程调度,厉害,很多人干了几年开发都不会,楼主加油。

论坛徽章:
0
5 [报告]
发表于 2014-12-02 16:32 |只看该作者
freshere 发表于 2014-12-01 16:26
谢谢你的回复,是啊,main 线程和 子线程 lock调用都成功返回了,但它们都操作的同一个mutex,问题是两个th ...


是我晕了,还是你晕了。。。^_^。
如果只有一个线程需要来访问这个mutex,mutex就没有存在的必要了。
mutex本来就是想让多个线程来做互斥访问。2个线程如果不是同时lock成功就对了。现在的现象是一个线程lock成功,退出lock,另外一个线程也是lock成功再退出lock,是正常的现象。

论坛徽章:
0
6 [报告]
发表于 2014-12-02 16:39 |只看该作者
问题是一个线程lock 成功了,并没有unlock,另外一个线程也lock 同一个mutex,也成功了!
回复 5# GreatChinaWolf


   

论坛徽章:
0
7 [报告]
发表于 2014-12-02 18:07 |只看该作者
回复 6# freshere


看懂你的问题了。刚刚看错重点了。

你考虑用sem_init,sem_post这一套而不是pthread mutex。


论坛徽章:
0
8 [报告]
发表于 2014-12-03 09:16 |只看该作者
谢谢您的解答,但是还是没太明白为什么同一把锁会锁两次? 改用semaphore的具体原因是?
回复 7# GreatChinaWolf


   

论坛徽章:
0
9 [报告]
发表于 2014-12-03 10:16 |只看该作者
回复 8# freshere


我以前貌似也碰到同样的问题,没有找到答案。
后来就是用sem这一套,而不是pthread mutex。

论坛徽章:
0
10 [报告]
发表于 2014-12-03 12:07 |只看该作者
谢谢Wolf的耐心帮助,点个大赞
回复 9# GreatChinaWolf


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP