- 论坛徽章:
- 0
|
pthread_delay_np
linux下是没有pthread_delay_np,在Solaries下才有.
可以试一下下面一段代码进行线程延时
Not sure is sleep(3) is threadsafe. However, there is a more appropriate
and portable way to do this in threads by using a condition.
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t
*mutex, const struct timespec *abstime);
Yes, you need to set a mutex.
- --- code frag --
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
int rv;
ts.tv_sec = 0;
ts.tv.nsec = 500000; /* 500,000 nanoseconds = 500 ms */
pthread_mutex_lock(&mymutex);
rv = pthread_cond_timedwait(&mycond, &mymutex, &ts);
switch(rv) {
case ETIMEDOUT:
/* Handle timeout */
case EINTR:
/* Interupted by signal *.
case EBUSY:
default:
/* Handle errors */
case 0:
/* condition received a condition signal */
}
pthread_mutex_unlock(&mymutex); |
|