LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3449|回复: 1

使用 pthread_cond_timewait() 来做延时的问题?

[复制链接]
发表于 2006-2-17 13:34:56 | 显示全部楼层 |阅读模式
我需要在一个线程中做延时,我是这样做的。
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

void mydelay()
{               
       struct timespec ts;
        int rv;
        ts.tv_sec = 1;
        ts.tv_nsec = 0; /* 40,000 nanoseconds = 40 ms */

        pthread_mutex_lock(&mymutex);
        
        pthread_cond_timedwait(&mycond, &mymutex, &ts);
      
        pthread_mutex_unlock(&mymutex);
}


在我的线程函数中,我调用了mydelay() 但是好像没有起作用,不知为什么??
发表于 2006-2-18 17:38:03 | 显示全部楼层
你最后一个参数搞错了

man pthread_cond_timedwait:
int   pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mutex, const struct timespec *abstime);

The abstime parameter specifies an abso-lute time, with the same origin  as  time(2)  and  gettimeofday(2):  an abstime of 0 corresponds to 00:00:00 GMT, January 1, 1970.
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表