Linux多线程编程 - sleep 和 pthread_cond_timedwait

摘要:多线程编程中,线程A循环计算,然后sleep一会接着计算(目的是减少CPU利用率);存在的问题是,如果要关闭程序,通常选择join线程A等待线程A退出,可是我们必须等到sleep函数返回,该线程A才能正常退出,这无疑减慢了程序退出的速度。当然,你可以terminate线程A,但这样做很不优雅,且会存在一些未知问题。采用pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t *mutex, const struct timespec * abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演示一个demo。

首先,看这段代码,thr_fn为一个线程函数:

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

采用pthread_cond_timedwait函数实现的如下:

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

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 and the condition variable and other access by threads to those objects through the pthread condition variable interfaces.)

如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。

等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。

尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。

注: 为了可靠的使用条件变量和确保不忘记对条件变量的唤醒操作,应该采用一个bool变量和mutex变量同条件变量配合使用。如本文demo。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值