Linux 多线程编程( POSIX )( 五 )----->代码区 ( 条件变量实例 )

1.条件变量的基本作用

//!> 条件变量的基本作用

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

//!> 注意此处为了方便就使用静态变量吧

pthread_mutex_t 
          mutex =PTHREAD_MUTEX_INITIALIZER;
                                    //!> 初始化互斥锁
pthread_cond_t            cond =PTHREAD_COND_INITIALIZER;
                                    //!> 初始化条件变量

int                                    g_i =0;

//!> 线程1入口
void * entrance_1( void * arg )
{
      for( g_i =1; g_i < 10; g_i++ )
      {
            pthread_mutex_lock( &mutex);            //!> 上锁
           
            if( g_i % 3== 0 )                                    //!> 给一个唤醒条件
            {
                  pthread_cond_signal( &cond);      //!> 执行唤醒
            }
            else
            {
                  printf("线程1在执行:%d\n", g_i);
            }
           
            pthread_mutex_unlock( &mutex);      //!> 解锁
           
            sleep( 1);
      }
}

//!> 线程2入口
void* entrance_2( void * arg )
{
      while( g_i< 10 )
      {
            pthread_mutex_lock( &mutex);            //!> 加锁
           
            if( g_i % 3!= 0 )
            {
                  pthread_cond_wait( &cond, &mutex);      //!> 等待
            }
           
            printf("线程2在执行:%d\n", g_i);
           
            pthread_mutex_unlock( &mutex);      //!> 解锁
           
            sleep( 1);
      }
}

int main( )
{
      pthread_t            tid1;      //!> 线程1
      pthread_t            tid2;      //!> 线程2
     
      if(pthread_create( &tid1, NULL, entrance_1, NULL ) !=0 )
      {
            printf("创建线程1失败....\n");
            exit(EXIT_FAILURE );
      }
       
      if(pthread_create( &tid2, NULL, entrance_2, NULL ) !=0 )
      {
              printf("创建线程2失败....\n");
            exit(EXIT_FAILURE );
      }

      pthread_join( tid1, NULL);      //!> 等待...
      pthread_join( tid2, NULL);      //!> 等待...
     
      pthread_mutex_destroy( &mutex);            //!> 销毁
      pthread_cond_destroy( &cond);            //!> 销毁
     
      return0;
}


      结果:
      线程1在执行:1
      线程1在执行:2
      线程2在执行:3                  //!> ------> 222
      线程1在执行:4
      线程1在执行:5
      线程2在执行:6                  //!>------>222     
      线程1在执行:7
      线程1在执行:8
      线程2在执行:9                  //!> ------>222


2.生产消费者

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define  MAX      5

pthread_mutex_t            mutex =PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t            cond =PTHREAD_COND_INITIALIZER;

typedef struct
{
      char      buf[MAX];
      int            count;
}BUFFER;

BUFFER share = { "", 0};      //!> 初始化
char ch = 'A';

void * _read_( void * arg )
{
      int n =0;
     
      printf("读:%d  开始....\n",(unsigned)pthread_self());
     
      while( ch !='Z' )
      {
            pthread_mutex_lock( &mutex);            //!> 加锁
           
            if(share.count != MAX)            //!> 不是最大就读,就是一次性读入
            {
                  share.buf[share.count++] =ch++;      //!> 读入
                  printf("读%d:当前字符 = %c\n", (unsigned)pthread_self(), ch-1);
                 
                  if(share.count == MAX )//!> 达到MAX,不读入
                  {
                        printf("读%d:缓冲区满!\n", (unsigned)pthread_self());
                        pthread_cond_signal( &cond);      //!> ??????
                     
            }
           
            pthread_mutex_unlock(&mutex);      //!> 解锁互斥量
      }
     
      sleep( 1);
      printf("读%d:退出咯~\n", (unsigned)pthread_self());

}

void * _write_( void * arg )
{
      int i, n =0;
     
      printf("写:%d  开始....\n",(unsigned)pthread_self());

      while( ch !='Z' )
      {
            pthread_mutex_lock( &mutex );
           
            printf("写%d:等待...\n", (unsigned)pthread_self());
            while(share.count != MAX )
            {
                  pthread_cond_wait( &cond, &mutex);      //!> 只要buf不满等待...
            }
           
            printf("写%d:开始写入...\n", (unsigned)pthread_self());
            for( i = 0;share.count > 0 ; i++, share.count-- )
            {
                  printf("%c  ",share.buf[i]);      //!> 输出看看...
                  if(share.count == 1 )
                  {
                        printf("\n");            //!> 换行格式而已
                  }
            }
           
            pthread_mutex_unlock( &mutex );
      }

      sleep( 1);     
      printf("写%d:退出...\n", (unsigned)pthread_self());
}

int main()
{
      pthread_t            tid_read,tid_write;
     
      if (pthread_create( &tid_read, NULL, _read_, NULL ) !=0 )
      {
            printf("创建线程1失败...\n");
            exit(EXIT_FAILURE );
      }

      if (pthread_create( &tid_write, NULL, _write_, NULL )!= 0 )
      {
            printf("创建线程2失败...\n");
            exit(EXIT_FAILURE );
      }
     
      pthread_join( tid_read, NULL );
      pthread_join( tid_write, NULL );
     
      pthread_mutex_destroy( &mutex );
      pthread_cond_destroy( &cond );
     
      return0;
}
     
/*
      读:1680848640  开始....
      读1680848640:当前字符 = A
      读1680848640:当前字符 = B
      读1680848640:当前字符 = C
      读1680848640:当前字符 = D
      读1680848640:当前字符 = E
      读1680848640:缓冲区满!
      写:1672455936  开始....
      写1672455936:等待...
      写1672455936:开始写入...
           
      写1672455936:等待...
      读1680848640:当前字符 = F
      读1680848640:当前字符 = G
      读1680848640:当前字符 = H
      读1680848640:当前字符 = I
      读1680848640:当前字符 = J
      读1680848640:缓冲区满!
      写1672455936:开始写入...
     
      写1672455936:等待...
      读1680848640:当前字符 = K
      读1680848640:当前字符 = L
      读1680848640:当前字符 = M
      读1680848640:当前字符 = N
      读1680848640:当前字符 = O
      读1680848640:缓冲区满!
      写1672455936:开始写入...
     
      写1672455936:等待...
      读1680848640:当前字符 = P
      读1680848640:当前字符 = Q
      读1680848640:当前字符 = R
      读1680848640:当前字符 = S
      读1680848640:当前字符 = T
      读1680848640:缓冲区满!
      写1672455936:开始写入...
     
      写1672455936:等待...
      读1680848640:当前字符 = U
      读1680848640:当前字符 = V
      读1680848640:当前字符 = W
      读1680848640:当前字符 = X
      读1680848640:当前字符 = Y
      读1680848640:缓冲区满!
      写1672455936:开始写入...
     
      写1672455936:退出...
      读1680848640:退出咯~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值