Chinaunix首页 | 论坛 | 博客
  • 博客访问: 807056
  • 博文数量: 321
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 936
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-23 11:25
文章分类

全部博文(321)

文章存档

2017年(1)

2016年(10)

2015年(61)

2014年(187)

2013年(62)

分类: C/C++

2015-02-07 10:50:43

条件变量的结构为pthread_cond_t,函数pthread_cond_init()被用来初始化一个条件变量。它的原型为:
  extern int pthread_cond_init __P ((pthread_cond_t *__cond,__const pthread_condattr_t *__cond_attr));
  其中cond是一个指向结构pthread_cond_t的指针,cond_attr是一个指向结构pthread_condattr_t的指 针。结构 pthread_condattr_t是条件变量的属性结构,和互斥锁一样我们可以用它来设置条件变量是进程内可用还是进程间可用,默认值是 PTHREAD_ PROCESS_PRIVATE,即此条件变量被同一进程内的各个线程使用。注意初始化条件变量只有未被使用时才能重新初始化或被释放。释放一个条件变量 的函数为pthread_cond_destroy(pthread_cond_t cond)。
  也可以静态的初始化条件变量
  pthread_cond_t my_condition = PTHREAD_COND_INITIALIZER;
  函数pthread_cond_wait()使线程阻塞在一个条件变量上。它的函数原型为:
  extern int pthread_cond_wait __P ((pthread_cond_t *__cond,pthread_mutex_t *__mutex));
  调用这个函数时,线程解开mutex指向的锁并被条件变量cond阻塞。线程可以被函数pthread_cond_signal和函数 pthread_cond_broadcast唤醒线程被唤醒后,它将重新检查判断条件是否满足,如果还不满足,一般说来线程应该仍阻塞在这里,被等待被 下一次唤醒。这个过程一般用while语句实现。
  通过下面的程序来理解:

点击(此处)折叠或打开

  1. #include <pthread.h>
  2.   #include <stdio.h>
  3.   #include <stdlib.h>
  4.   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*初始化互斥锁*/
  5.   pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //初始化条件变量
  6.   void *thread1(void *);
  7.   void *thread2(void *);
  8.   int i=1;
  9.   int main(void)
  10.   {
  11.   pthread_t t_a;
  12.   pthread_t t_b;
  13.   pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
  14.   pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
  15.   pthread_join(t_b, NULL);/*等待进程t_b结束*/
  16.   pthread_mutex_destroy(&mutex);
  17.   pthread_cond_destroy(&cond);
  18.   exit(0);
  19.   }
  20.   void *thread1(void *junk)
  21.   {
  22.   for(i=1;i<=9;i++)
  23.   {
  24.   printf("IN one\n");
  25.   pthread_mutex_lock(&mutex);//
  26.   if(i%3==0)
  27.   pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
  28.   else
  29.   printf("thead1:%d\n",i);
  30.   pthread_mutex_unlock(&mutex);//*解锁互斥量*/
  31.   printf("Up Mutex\n");
  32.   sleep(10);
  33.   }
  34.   }
  35.   void *thread2(void *junk)
  36.   {
  37.   while(i<9)
  38.   {
  39.   printf("IN two \n");
  40.   pthread_mutex_lock(&mutex);
  41.   if(i%3!=0)
  42.   pthread_cond_wait(&cond,&mutex);/*等待*/
  43.   printf("thread2:%d\n",i);
  44.   pthread_mutex_unlock(&mutex);
  45.   printf("Down Mutex\n");
  46.   sleep(10);
  47.   }
  48.   }

  
  ----------------------------
  IN one
  thead1:1
  Up  Mutex
  IN two
  IN one
  thead1:2
  Up  Mutex
  IN one
  Up  Mutex
  thread2:3
  Down  Mutex
  IN one
  thead1:4
  Up  Mutex
  IN two
  IN one
  thead1:5
  Up  Mutex
  IN one
  Up  Mutex
  thread2:6
  Down  Mutex
  IN one
  thead1:7
  Up  Mutex
  IN two
  IN one
  thead1:8
  Up  Mutex
  IN one
  Up  Mutex
  thread2:9
  Down  Mutex
  -------
  总结
  pthread_cond_wait()是在当点  unlock 等待常量,其他线程就可以得到锁了,当得到signal时候会又会lock,,,,,继续往当点走,直到unlock
阅读(5978) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~