linux多线程编程,替代sleep的几种方式

来源:互联网 发布:linux 7 配置ip地址 编辑:程序博客网 时间:2024/06/12 01:24

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为一个线程函数:

[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3.   
  4. int flag = 1;  
  5. void * thr_fn(void * arg) {  
  6.   while (flag){  
  7.     printf("******\n");  
  8.     sleep(10);  
  9.   }  
  10.   printf("sleep test thread exit\n");  
  11. }  
  12.    
  13. int main() {  
  14.   pthread_t thread;  
  15.   if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {  
  16.     printf("error when create pthread,%d\n", errno);  
  17.     return 1;  
  18.   }  
  19.    
  20.   char c ;  
  21.   while ((c = getchar()) != 'q');  
  22.    
  23.   printf("Now terminate the thread!\n");  
  24.   flag = 0;  
  25.   printf("Wait for thread to exit\n");  
  26.   pthread_join(thread, NULL);  
  27.   printf("Bye\n");  
  28.   return 0;  
  29. }  
   输入q后,需要等线程从sleep中醒来(由挂起状态变为运行状态),即最坏情况要等10s,线程才会被join。采用sleep的缺点:不能及时唤醒线程。
采用pthread_cond_timedwait函数实现的如下:

[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <sys/time.h>   
  3. #include <unistd.h>   
  4. #include <pthread.h>   
  5. #include <errno.h>   
  6.    
  7. static pthread_t thread;  
  8. static pthread_cond_t cond;  
  9. static pthread_mutex_t mutex;  
  10. static int flag = 1;  
  11.    
  12. void * thr_fn(void * arg)   
  13. {  
  14.   struct timeval now;  
  15.   struct timespec outtime;  
  16.   pthread_mutex_lock(&mutex);  
  17.   while (flag) {  
  18.     printf("*****\n");  
  19.     gettimeofday(&now, NULL);  
  20.     outtime.tv_sec = now.tv_sec + 5;  
  21.     outtime.tv_nsec = now.tv_usec * 1000;  
  22.     pthread_cond_timedwait(&cond, &mutex, &outtime);  
  23.   }  
  24.   pthread_mutex_unlock(&mutex);  
  25.   printf("cond thread exit\n");  
  26. }  
  27.    
  28. int main(void)   
  29. {  
  30.   pthread_mutex_init(&mutex, NULL);  
  31.   pthread_cond_init(&cond, NULL);  
  32.   if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {  
  33.     printf("error when create pthread,%d\n", errno);  
  34.     return 1;  
  35.   }  
  36.   char c ;  
  37.   while ((c = getchar()) != 'q');  
  38.   printf("Now terminate the thread!\n");  
  39.   
  40.   pthread_mutex_lock(&mutex);  
  41.   flag = 0;  
  42.   pthread_cond_signal(&cond);  
  43.   pthread_mutex_unlock(&mutex);  
  44.   printf("Wait for thread to exit\n");  
  45.   pthread_join(thread, NULL);  
  46.   printf("Bye\n");  
  47.   return 0;  
  48. }  

        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结构体。