pthread_cond_timedwait死锁不返回什么原因?

charryli 2014-05-27 10:00:55
操作系统是linux debian:Linux debian 2.6.18-6-686 #1 SMP Mon Oct 13 16:13:09 UTC 2008 i686 GNU/Linux
代码如下:
struct timeval now;
gettimeofday(&now, NULL);
printf("*** in wait now= %d:%d<%d>\n", now.tv_sec, now.tv_usec,__LINE__);//时间显示正确

struct timespec abstime ;
abstime.tv_sec = now.tv_sec ;
abstime.tv_nsec = now.tv_usec + 10*1000 ;//超时10毫秒

printf("arrive here<%d>\n", __LINE__);//可以打印
pthread_mutex_lock(&m_p_event->m_mutex);
printf("arrive here<%d>\n", __LINE__);//可以打印
ret = pthread_cond_timedwait(&m_p_event->m_cond,&m_p_event->m_mutex,&abstime) ;
printf("arrive here<%d>\n", __LINE__);//无法打印
pthread_mutex_unlock(&m_p_event->m_mutex);

请教各位:是什么原因呢?
...全文
577 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
SoldierJazz2021 2016-12-13
  • 打赏
  • 举报
回复
5楼的描述是有问题的,没有pthread_cond_timedwait原理没有弄清楚,需要更正以下,可以看看官方说明;
SoldierJazz2021 2016-12-13
  • 打赏
  • 举报
回复 1
注意,包含你mutex和cond的结构体不要使用 __attribute__(packed); 你可以直接尝试 函数内 定义一个 mutex 与 cond ,应该就可以了。
lvzg2005 2016-11-11
  • 打赏
  • 举报
回复
pthread_mutex_lock(&mutex); ret = pthread_cond_timedwait(&cond,&mutex,&abstime) ; 这两句死锁。pthread_cond_timedwait需要没有线程加锁mutex时候才能执行,或者说根本不是在等信号量,是在等锁资源。
静之深 2014-05-30
  • 打赏
  • 举报
回复
引用 2 楼 charryli 的回复:
不行啊,修改成5秒,依然卡住无返回。我使用sleep(1),可以通过,但是实现不了预计的功能。 请问会不会是debian操作系统安装的问题呢?或者缺少什么组件或服务?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>


int main()
{
    int ret=-1;
    pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  cond=PTHREAD_COND_INITIALIZER;

    struct timeval  now;
    gettimeofday(&now, NULL);
    printf("*** in wait now= %d:%d<%d>\n", now.tv_sec, now.tv_usec,__LINE__);//时间显示正确

    struct timespec abstime ;
    abstime.tv_sec = now.tv_sec+1 ;
    abstime.tv_nsec = now.tv_usec + 10*1000 ;//超时10毫秒

    printf("arrive here<%d>\n", __LINE__);//可以打印
    pthread_mutex_lock(&mutex);
    printf("arrive here<%d>\n", __LINE__);//可以打印    
    ret = pthread_cond_timedwait(&cond,&mutex,&abstime) ;
    printf("arrive here<%d>\n", __LINE__);//无法打印    
    pthread_mutex_unlock(&mutex);


    return ret;
}



yingc@yingc:~/gcyin/test/tmp/csdn$ gcc -D_REENTRANT cond.c  -o condtest  -lpthread
cond.c: In function ‘main’:
cond.c:32:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__time_t’ [-Wformat]
cond.c:32:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘__suseconds_t’ [-Wformat]
yingc@yingc:~/gcyin/test/tmp/csdn$ ls
cond.c  condtest  libtestlib.a  main  main.cpp  main.o  testlib.cpp  testlib.h  testlib.o
yingc@yingc:~/gcyin/test/tmp/csdn$ ./condtest 
*** in wait now= 1401418200:911168<32>
arrive here<38>
arrive here<40>
arrive here<42>
yingc@yingc:~/gcyin/test/tmp/csdn$ fg
我的系统是ubuntu
静之深 2014-05-30
  • 打赏
  • 举报
回复
引用 2 楼 charryli 的回复:
不行啊,修改成5秒,依然卡住无返回。我使用sleep(1),可以通过,但是实现不了预计的功能。 请问会不会是debian操作系统安装的问题呢?或者缺少什么组件或服务?
你把完整的程序及编译命令发出来看看
charryli 2014-05-30
  • 打赏
  • 举报
回复
不行啊,修改成5秒,依然卡住无返回。我使用sleep(1),可以通过,但是实现不了预计的功能。 请问会不会是debian操作系统安装的问题呢?或者缺少什么组件或服务?
静之深 2014-05-27
  • 打赏
  • 举报
回复
看着比较奇怪,你把超时时间延长下看看: gettimeofday(&now, NULL); outtime.tv_sec = now.tv_sec + 5; outtime.tv_nsec = now.tv_usec * 1000; pthread_cond_timedwait(&cond, &mutex, &outtime);

23,119

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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