当前位置: 技术问答>linux和unix
谁知道条件变量的用法??用 pthread_cond_timedwait()时遇到了问题
来源: 互联网 发布时间:2015-01-20
本文导语: int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); 用此函数,没有signal时,线程并没有阻塞abstime ,而是马上返回 ETIMEDOUT 即超时 为什么?? 应该怎么用? 谁能给个例...
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex, const struct timespec *abstime);
用此函数,没有signal时,线程并没有阻塞abstime ,而是马上返回 ETIMEDOUT 即超时
为什么??
应该怎么用?
谁能给个例子??
谢谢!!
pthread_mutex_t *mutex, const struct timespec *abstime);
用此函数,没有signal时,线程并没有阻塞abstime ,而是马上返回 ETIMEDOUT 即超时
为什么??
应该怎么用?
谁能给个例子??
谢谢!!
|
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;
}