http://blog.csdn.net/wind19/article/details/7541811
linux 下面的sleep,usleep,nanosleep 和select比较
sleep 时间单位是秒
usleep的时间单位是微秒
select的精度是微妙,精确
struct timeval delay;
delay.tv_sec = 0;
delay.tv_usec = 20 * 1000; // 20 ms
select(0, NULL, NULL, NULL, &delay);
usleep()有有很大的问题
- 在一些平台下不是线程安全,如HP-UX以及Linux
- usleep()会影响信号
- 在很多平台,如HP-UX以及某些Linux下,当参数的值必须小于1 * 1000 * 1000也就是1秒,否则该函数会报错,并且立即返回。
- 大部分平台的帮助文档已经明确说了,该函数是已经被舍弃的函数。
还好,POSIX规范中有一个很好用的函数,nanosleep(),该函数没有usleep()的这些缺点,它的精度是纳秒级。在Solaris的多线程环境下编译器会自动把usleep()连接成nanosleep()。
Linux下短延时推荐使用select函数,因为准确.
******************************************************************************************
http://blog.csdn.net/horstlinux/article/details/7911457
我只想要进程的某个线程休眠一段时间的,可是用sleep()是将整个进程都休眠的,这个可能就达不到,我们想要的效果了。 目前我知道有三种方式:
1 usleep
这个是轻量级的, 听说能可一实现线程休眠, 我个人并不喜欢这种方式,所以我没有验证它的可行信(个人不推荐)。
2 select
这个可以,我也用过这种方式, 它是在轮询。
3 pthread_cond_timedwait
采用pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t *mutex, const struct timespec *abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演示一个demo。
首先,看这段代码,thr_fn为一个线程函数:
- #include <stdio.h>
- #include <stdlib.h>
- int flag = 1;
- void * thr_fn(void * arg) {
- while (flag){
- printf("******\n");
- sleep(10);
- }
- printf("sleep test thread exit\n");
- }
- int main() {
- pthread_t thread;
- if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {
- printf("error when create pthread,%d\n", errno);
- return 1;
- }
- char c ;
- while ((c = getchar()) != 'q');
- printf("Now terminate the thread!\n");
- flag = 0;
- printf("Wait for thread to exit\n");
- pthread_join(thread, NULL);
- printf("Bye\n");
- return 0;
- }
采用pthread_cond_timedwait函数实现的如下:
- #include <stdio.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <errno.h>
- static pthread_t thread;
- static pthread_cond_t cond;
- static pthread_mutex_t mutex;
- static int flag = 1;
- void * thr_fn(void * arg)
- {
- struct timeval now;
- struct timespec outtime;
- pthread_mutex_lock(&mutex);
- while (flag) {
- printf("*****\n");
- gettimeofday(&now, NULL);
- outtime.tv_sec = now.tv_sec + 5;
- outtime.tv_nsec = now.tv_usec * 1000;
- pthread_cond_timedwait(&cond, &mutex, &outtime);
- }
- pthread_mutex_unlock(&mutex);
- printf("cond thread exit\n");
- }
- int main(void)
- {
- pthread_mutex_init(&mutex, NULL);
- pthread_cond_init(&cond, NULL);
- if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {
- printf("error when create pthread,%d\n", errno);
- return 1;
- }
- char c ;
- while ((c = getchar()) != 'q');
- printf("Now terminate the thread!\n");
- pthread_mutex_lock(&mutex);
- flag = 0;
- pthread_cond_signal(&cond);
- pthread_mutex_unlock(&mutex);
- printf("Wait for thread to exit\n");
- pthread_join(thread, NULL);
- printf("Bye\n");
- return 0;
- }
pthread_cond_timedwait()函数阻塞住调用该函数的线程,等待由cond指定的条件被触发(pthread_cond_broadcast() or pthread_cond_signal())。
当pthread_cond_timedwait()被调用时,调用线程必须已经锁住了mutex。函数pthread_cond_timedwait()会对mutex进行【解锁和执行对条件的等待】(原子操作)。这里的原子意味着:解锁和执行条件的等待是原则的,一体的。(In this case, atomically means with respect to the mutex andthe condition variable and other access by threads to those objectsthrough the pthread condition variable interfaces.)
如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。
等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。
尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。