当前位置: 技术问答>linux和unix
关于线程休眠的问题
来源: 互联网 发布时间:2016-02-06
本文导语: 以下代码可以使当前线程休眠1秒钟后继续往下执行,但当要休眠毫秒时,不知改怎样些,比如要线程休眠600毫秒。 pthread_cond_t mycond = PTHREAD_COND_INITIALIZER; pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; struct timespec ts...
以下代码可以使当前线程休眠1秒钟后继续往下执行,但当要休眠毫秒时,不知改怎样些,比如要线程休眠600毫秒。
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
ts.tv_sec = time(NULL)+1;
ts.tv_nsec = 0;
pthread_mutex_lock(&mymutex);
pthread_cond_timedwait(&mycond, &mymutex ,(const struct timespec *)&ts);
pthread_mutex_unlock(&mymutex);
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
ts.tv_sec = time(NULL)+1;
ts.tv_nsec = 0;
pthread_mutex_lock(&mymutex);
pthread_cond_timedwait(&mycond, &mymutex ,(const struct timespec *)&ts);
pthread_mutex_unlock(&mymutex);
|
select
行不?
行不?