23,119
社区成员
发帖
与我相关
我的任务
分享
#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