Linux条件变量用法


Linux条件变量是线程中的东西,就是等待某一条件的发生,和信号一样。

以下是说明,条件变量使我们可以睡眠等待某种条件出现。

条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。

条件变量类型为pthread_cond_t

创建和注销
条件变量和互斥锁一样,都有静态动态两种创建方式,静态方式使用PTHREAD_COND_INITIALIZER常量,如下:

pthread_cond_t cond=PTHREAD_COND_INITIALIZER

动态方式调用pthread_cond_init()函数,API定义如下:

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)

尽管POSIX标准中为条件变量定义了属性,但在LinuxThreads中没有实现,因此cond_attr值通常为NULL,且被忽略。

注销一个条件变量需要调用pthread_cond_destroy(),只有在没有线程在该条件变量上等待的时候才能注销这个条件变量,否则返回EBUSY。API定义如下:

int pthread_cond_destroy(pthread_cond_t *cond)

等待和激发
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)

等待条件有两种方式:无条件等待pthread_cond_wait()和计时等待pthread_cond_timedwait(),其中计时等待方式如果在给定时刻前条件没有满足,则返回ETIMEOUT,结束等待,其中abstime以与time()系统调用相同意义的绝对时间形式出现,0 表示格林尼治时间1970年1月1日0时0分0秒。

使用绝对时间而非相对时间的优点是。如果函数提前返回(很可能因为捕获了一个信号)

无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求pthread_cond_wait()(或 pthread_cond_timedwait(),下同)的竞争条件(Race Condition)。mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),且在调用 pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入 pthread_cond_wait()前的加锁动作对应。

激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;而pthread_cond_broadcast()则激活所有等待线程。

其他
pthread_cond_wait()和pthread_cond_timedwait()都被实现为取消点,因此,在该处等待的线程将立即重新运行,在重新锁定mutex后离开pthread_cond_wait(),然后执行取消动作。也就是说如果pthread_cond_wait()被取消,mutex是保持锁定状态的,因而需要定义退出回调函数来为其解锁。

以下示例集中演示了互斥锁和条件变量的结合使用,以及取消对于条件等待动作的影响。在例子中,有两个线程被启动,并等待同一个条件变量,如果不使用退出回调函数(见范例中的注释部分),则tid2将在pthread_mutex_lock()处永久等待。如果使用回调函数,则tid2的条件等待及主线程的条件激发都能正常工作。

  1. 例子  
  2.   
  3. #include <stdio.h>;   
  4.   
  5. #include <pthread.h>;   
  6.   
  7. #include <unistd.h>;   
  8.   
  9.   
  10.   
  11. pthread_mutex_t mutex;  
  12.   
  13. pthread_cond_t  cond;  
  14.   
  15.   
  16.   
  17. void * child1(void *arg)  
  18.   
  19. {  
  20.   
  21.         pthread_cleanup_push(pthread_mutex_unlock,&mutex);  /* comment 1 */  
  22.   
  23.         while(1){  
  24.   
  25.                printf("thread 1 get running \n");  
  26.   
  27.                printf("thread 1 pthread_mutex_lock returns %d\n",pthread_mutex_lock(&mutex));  
  28.   
  29.                pthread_cond_wait(&cond,&mutex);// 这里正在等待信号,此时条件变量必须与一个互斥锁关联,   
  30.   
  31.                printf("thread 1 condition applied\n");  
  32.   
  33.                pthread_mutex_unlock(&mutex);  
  34.   
  35.                sleep(5);  
  36.   
  37.         }  
  38.   
  39.         pthread_cleanup_pop(0);     /* comment 2 */  
  40.   
  41. }  
  42. <span style="font-size: 14px; font-family: 'Arial,Microsoft Yahei,Simsun,sans-serif'; "></span><pre>int main(void)  
  43.   
  44. {  
  45.   
  46.         int tid1,tid2;  
  47.   
  48.   
  49.   
  50.         printf("hello, condition variable test\n");  
  51.   
  52.         pthread_mutex_init(&mutex,NULL);  
  53.   
  54.         pthread_cond_init(&cond,NULL);  
  55.   
  56.         pthread_create(&tid1,NULL,child1,NULL);  
  57.   
  58.         pthread_create(&tid2,NULL,child2,NULL);  
  59.   
  60.         do{  
  61.   
  62.               sleep(2);                   /* comment 4 */  
  63.   
  64.               pthread_cancel(tid1);       /* comment 5 */  
  65.   
  66.               sleep(2);                   /* comment 6 */  
  67.   
  68.               pthread_cond_signal(&cond);       //发送信号   
  69.   
  70.         }while(1);    
  71.   
  72.         sleep(100);  
  73.   
  74.         pthread_exit(0);  
  75.   
  76. }   

child1函数给出的是标准的条件变量的使用方式:回调函数保护,等待条件前锁定,pthread_cond_wait()返回后解锁。

相关内容