当前位置: 技术问答>linux和unix
pthread_cond_timedwait带时间的线程同步条件变量用法,请教!谢谢
来源: 互联网 发布时间:2016-02-27
本文导语: 子线程的部分代码如下: struct timespec timeout; int status; time_t tmp; while(1) { time(&tmp); printf("time = %dn",tmp); printf("this time is %s",ctime(&tmp)); timeout.tv_sec ...
子线程的部分代码如下:
struct timespec timeout;
int status;
time_t tmp;
while(1)
{
time(&tmp);
printf("time = %dn",tmp);
printf("this time is %s",ctime(&tmp));
timeout.tv_sec = tmp + 10;
timeout.tv_nsec = 0;
printf("timeout.tv_sec = %dn",timeout.tv_sec);
pthread_mutex_lock(&mutex);
status = pthread_cond_timedwait (&cond, &mutex, &timeout);
pthread_mutex_unlock(&mutex);
sleep(1);
}
----------------------------------
系统时钟正常。
没有线程在调用函数是条件变量 cond有效,因此
status = pthread_cond_timedwait (&cond, &mutex, &timeout);
要阻塞等待10秒后由于超时而返回,但是此函数却是立即返回的。
且返回值 有时为 553250有时为-11532016。errno=2.
请问该怎么修改代码才能正常使用status = pthread_cond_timedwait (&cond, &mutex, &timeout);
这个带超时时间的条件等待函数。谢谢
struct timespec timeout;
int status;
time_t tmp;
while(1)
{
time(&tmp);
printf("time = %dn",tmp);
printf("this time is %s",ctime(&tmp));
timeout.tv_sec = tmp + 10;
timeout.tv_nsec = 0;
printf("timeout.tv_sec = %dn",timeout.tv_sec);
pthread_mutex_lock(&mutex);
status = pthread_cond_timedwait (&cond, &mutex, &timeout);
pthread_mutex_unlock(&mutex);
sleep(1);
}
----------------------------------
系统时钟正常。
没有线程在调用函数是条件变量 cond有效,因此
status = pthread_cond_timedwait (&cond, &mutex, &timeout);
要阻塞等待10秒后由于超时而返回,但是此函数却是立即返回的。
且返回值 有时为 553250有时为-11532016。errno=2.
请问该怎么修改代码才能正常使用status = pthread_cond_timedwait (&cond, &mutex, &timeout);
这个带超时时间的条件等待函数。谢谢
|
abstime要使用绝对时间,否则可能会出现你所说的情况。
#include
#include
#include
#include
pthread_cond_t mycond;
pthread_mutex_t mymutex;
int mydata;
void *myfun1(void *)
{
timespec mytime;
while(1)
{
mytime.tv_sec = time(NULL)+1; //Wait for 1 second, Must
mytime.tv_nsec = 0;
int ret;
pthread_mutex_lock(&mymutex);
// pthread_cond_wait(&mycond, &mymutex);
ret = pthread_cond_timedwait(&mycond, &mymutex,(const struct timespec *)&mytime);
if( ret != 0 )
{
printf("timeout in %dn",pthread_self());
pthread_mutex_unlock(&mymutex);
continue;
}
while(mydata)
{
printf("consume in %d (mydata = %d)n",pthread_self(),mydata);
mydata--;
}
pthread_mutex_unlock(&mymutex);
}
}
void *myfun2(void *)
{
while(1)
{
pthread_mutex_lock(&mymutex);
printf("produce in %dn",pthread_self());
mydata++;
pthread_mutex_unlock(&mymutex);
pthread_cond_signal(&mycond);
sleep(3);
}
}
int main()
{
mydata = 0;
pthread_t mythread1,mythread2,mythread3;
pthread_cond_init(&mycond,NULL);
pthread_mutex_init(&mymutex,NULL);
pthread_create(&mythread1,NULL,myfun1,NULL);
pthread_create(&mythread2,NULL,myfun2,NULL);
// pthread_create(&mythread3,NULL,myfun1,NULL);
// pthread_create(&mythread3,NULL,myfun2,NULL);
pthread_join(mythread1,NULL);
pthread_join(mythread2,NULL);
// pthread_join(mythread3,NULL);
return 0;
}
#include
#include
#include
#include
pthread_cond_t mycond;
pthread_mutex_t mymutex;
int mydata;
void *myfun1(void *)
{
timespec mytime;
while(1)
{
mytime.tv_sec = time(NULL)+1; //Wait for 1 second, Must
mytime.tv_nsec = 0;
int ret;
pthread_mutex_lock(&mymutex);
// pthread_cond_wait(&mycond, &mymutex);
ret = pthread_cond_timedwait(&mycond, &mymutex,(const struct timespec *)&mytime);
if( ret != 0 )
{
printf("timeout in %dn",pthread_self());
pthread_mutex_unlock(&mymutex);
continue;
}
while(mydata)
{
printf("consume in %d (mydata = %d)n",pthread_self(),mydata);
mydata--;
}
pthread_mutex_unlock(&mymutex);
}
}
void *myfun2(void *)
{
while(1)
{
pthread_mutex_lock(&mymutex);
printf("produce in %dn",pthread_self());
mydata++;
pthread_mutex_unlock(&mymutex);
pthread_cond_signal(&mycond);
sleep(3);
}
}
int main()
{
mydata = 0;
pthread_t mythread1,mythread2,mythread3;
pthread_cond_init(&mycond,NULL);
pthread_mutex_init(&mymutex,NULL);
pthread_create(&mythread1,NULL,myfun1,NULL);
pthread_create(&mythread2,NULL,myfun2,NULL);
// pthread_create(&mythread3,NULL,myfun1,NULL);
// pthread_create(&mythread3,NULL,myfun2,NULL);
pthread_join(mythread1,NULL);
pthread_join(mythread2,NULL);
// pthread_join(mythread3,NULL);
return 0;
}