条件变量的使用

关于条件变量的使用网上有很多的,大部分是使用生产和消费的模型来说明的,或者是哲学家吃饭的问题。
本着学以致用,不断创新的思想,我也调了一个模型。
大概是描述是这样的。

1.创建多个线程;
2.每个线程都是消费者,可能是最后一个消费者,也可能不是最后一个消费者,总之我们关心最后一个消费者,因为它要做些擦屁股的工作;
3.如果不是最后一个消费者,那就该吃吃就行了;

这个场景我也不知道什么时候会用,但是我现在的项目中会用到,如记录一下,示例代码如下:

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdint.h>


typedef struct
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    uint32_t value;
}shareddata_t;

shareddata_t shareddata;

uint32_t prechannel=0;
uint32_t channel=7;

void *consume(void *ptr)
{
  uint32_t *idx=(uint32_t *)ptr;
  uint32_t cnt=0;

  printf("Enter consume thread [%d] \n",*idx);
  while(1)
  {
    pthread_mutex_lock(&shareddata.mutex);

    //0.标记通道
    channel &= ~(1<<(*idx));

    //1.复制数据
    printf("channel [%d] copying data.---cnt[%d]---->\n",*idx,cnt++);

    //2.判断整体通道是否已经取完数据
    if( channel == 0)
    {
      //2.1 已经取完数据,说明现在的这个通道是最后一个通道
      //因为线程是不按顺序调度的。

      //2.2 给其他通道发送信号,告诉它们可以继续执行了。
      pthread_cond_broadcast(&shareddata.cond);
      printf("[%d] send broadcast.\n",*idx);
      prechannel |= (1<<(*idx));
    }
    else
    {
      //2.3进入到这里说明本线程不是最后一个获取数据的线程
      //既然不是最后一个线程,那就进入睡眠状态,等待最后一个线程来发送信号叫醒。
      printf("channel [%d] enter cond-wait\n",*idx);
      while(0!=channel)
      {
        pthread_cond_wait(&shareddata.cond,&shareddata.mutex);
      }
      printf("channel [%d] exit cond-wait\n",*idx);

      printf("enable channel [%d] again.\n",*idx);
      prechannel |= (1<<(*idx));
      if(prechannel==7)
      {
        channel=7;
        prechannel=0;
      }
      printf("prechannel=[0x%x],channel=[0x%x]\n\n",prechannel,channel);
    }


    pthread_mutex_unlock(&shareddata.mutex);
    sleep(1);
  }

  return NULL;
}


int main(void)
{
  uint32_t i=0;
  uint32_t idx[10];
  pthread_t tid[10];
  pthread_t protid;
  void *retval=NULL;

  pthread_mutex_init(&shareddata.mutex,NULL);
  pthread_cond_init(&shareddata.cond,NULL);

  for(i=0;i<3;i++)
  {
    idx[i]=i;
    pthread_create(&tid[i],NULL,consume,&idx[i]);
  }

  for(i=0;i<3;i++)
  {
    pthread_join(tid[i],&retval);
  }



  return 0;
}

最后运行的结果:

zz@server5:testcond$ ./test 
Enter consume thread [0] 
channel [0] copying data.---cnt[0]---->
channel [0] enter cond-wait
Enter consume thread [2] 
channel [2] copying data.---cnt[0]---->
Enter consume thread [1] 
channel [2] enter cond-wait
channel [1] copying data.---cnt[0]---->
[1] send broadcast.
channel [0] exit cond-wait
enable channel [0] again.
prechannel=[0x3],channel=[0x0]

channel [2] exit cond-wait
enable channel [2] again.
prechannel=[0x0],channel=[0x7]

channel [2] copying data.---cnt[1]---->
channel [2] enter cond-wait
channel [1] copying data.---cnt[1]---->
channel [1] enter cond-wait
channel [0] copying data.---cnt[1]---->
[0] send broadcast.
channel [2] exit cond-wait
enable channel [2] again.
prechannel=[0x5],channel=[0x0]

channel [1] exit cond-wait
enable channel [1] again.
prechannel=[0x0],channel=[0x7]

channel [0] copying data.---cnt[2]---->
channel [0] enter cond-wait
channel [1] copying data.---cnt[2]---->
channel [1] enter cond-wait
channel [2] copying data.---cnt[2]---->
[2] send broadcast.
channel [0] exit cond-wait
enable channel [0] again.
prechannel=[0x5],channel=[0x0]

channel [1] exit cond-wait
enable channel [1] again.
prechannel=[0x0],channel=[0x7]

channel [1] copying data.---cnt[3]---->
channel [1] enter cond-wait
channel [0] copying data.---cnt[3]---->
channel [0] enter cond-wait
channel [2] copying data.---cnt[3]---->
[2] send broadcast.
channel [1] exit cond-wait
enable channel [1] again.
prechannel=[0x6],channel=[0x0]

channel [0] exit cond-wait
enable channel [0] again.
prechannel=[0x0],channel=[0x7]

channel [2] copying data.---cnt[4]---->
channel [2] enter cond-wait
channel [1] copying data.---cnt[4]---->
channel [1] enter cond-wait
channel [0] copying data.---cnt[4]---->
[0] send broadcast.
channel [2] exit cond-wait
enable channel [2] again.
prechannel=[0x5],channel=[0x0]

channel [1] exit cond-wait
enable channel [1] again.
prechannel=[0x0],channel=[0x7]

channel [2] copying data.---cnt[5]---->
channel [2] enter cond-wait
channel [0] copying data.---cnt[5]---->
channel [0] enter cond-wait
channel [1] copying data.---cnt[5]---->
[1] send broadcast.
channel [2] exit cond-wait
enable channel [2] again.
prechannel=[0x6],channel=[0x0]

channel [0] exit cond-wait
enable channel [0] again.
prechannel=[0x0],channel=[0x7]

channel [1] copying data.---cnt[6]---->
channel [1] enter cond-wait
channel [2] copying data.---cnt[6]---->
channel [2] enter cond-wait
channel [0] copying data.---cnt[6]---->
[0] send broadcast.
channel [1] exit cond-wait
enable channel [1] again.
prechannel=[0x3],channel=[0x0]

channel [2] exit cond-wait
enable channel [2] again.
prechannel=[0x0],channel=[0x7]

其他消费线程在消费完之后会进入条件变量的等待状态,最后一个消费的线程负责向它们发送同步信号,其他线程接收到同步信号后会继续运行。在本例中继续运行的操作就是重新初始化自己的状态。这里使用了channel和prechannel来标记他们。当channel对应的bit为1表示这个线程还没有进行消费,为0表示已经消费。prechannel是用来辅助实现channel的重新初始化的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值