1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <pthread.h>
 4 
 5 pthread_mutex_t mutex;
 6 pthread_cond_t cond_full;
 7 pthread_cond_t cond_empty;
 8 
 9 int g_iBufSize = ;
 
 void *thread_producer(void* arg)
 {
     while(true)
     {
         printf("thread_producer:pthread_mutex_lock\n");
         pthread_mutex_lock(&mutex);
         // 拿到lock就可以访问,不会冲突,但是不一定满足条件,cond是为了在满足条件的时候通知另一个进程
         if( != g_iBufSize) //如果条件不满足就调用wait函数等待条件满足
         {
             printf("thread_producer:pthread_cond_wait cond_empty \n");
             pthread_cond_wait(&cond_empty, &mutex); // 这句话的前提是先有锁,这时候会自动先解锁,等到时机来临在加锁
         }
         
         //前面的wait操作已经包含了枷锁,这里直接访问
         printf("thread_producer>>>>>\n");
         g_iBufSize = ;
         
         printf("thread_producer: pthread_cond_signal\n");
         pthread_cond_signal(&cond_full);
    
         pthread_mutex_unlock(&mutex);
     }
     return NULL;
 }
 
 void *thread_consumer(void* arg)
 {
     while(true)
     {
         printf("thread_consumer:pthread_mutex_lock\n");
         pthread_mutex_lock(&mutex);
         if( == g_iBufSize)
         {
             printf("thread_consumer: pthread_cond_wait\n");
             pthread_cond_wait(&cond_full, &mutex);
         }
         
         printf("thread_consumer>>>\n");
         g_iBufSize = ;
         
         printf("thread_consumer: pthread_cond_signal\n");
         pthread_cond_signal(&cond_empty);
         
         pthread_mutex_unlock(&mutex);
 
     }
     return NULL;
 }
 
 //g++ -o bin1 -lpthread mutithread.cpp
 
 int main()
 {
     void *retval1, *retval2;
     pthread_t thread_id_1, thread_id_2;
     
     pthread_mutex_init(&mutex, NULL);
     pthread_cond_init(&cond_full, NULL);
     pthread_cond_init(&cond_empty, NULL);
     
     pthread_cond_signal(&cond_empty);
     pthread_create(&thread_id_1, NULL, thread_producer, NULL);//int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg);
     pthread_create(&thread_id_2, NULL, thread_consumer, NULL);
      
     pthread_join(thread_id_1, &retval1); // 阻塞等线程执行完
     pthread_join(thread_id_2, &retval2);
     
     pthread_cond_destroy(&cond_full);
     pthread_cond_destroy(&cond_empty);
     pthread_mutex_destroy(&mutex);
     
     return ;
 }

linux ptheard 生产者消费者的更多相关文章

  1. Linux 信号量 生产者消费者小例题

    菜鸟偶遇信号量,擦出火花(只有不熟才会有火花).于是上网搜资料和看<Unix环境高级编程>实现了几个小例题,高手请勿喷!这几位写得非常好啊: 题目来源: http://www.it165. ...

  2. linux进程 生产者消费者

    #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> # ...

  3. Linux 进程间通信(包含一个经典的生产者消费者实例代码)

    前言:编写多进程程序时,有时不可避免的需要在多个进程之间传递数据,我们知道,进程的用户的地址空间是独立,父进程中对数据的修改并不会反映到子进程中,但内核是共享的,大多数进程间通信方式都是在内核中建立一 ...

  4. linux下多线程互斥量实现生产者--消费者问题和哲学家就餐问题

    生产者消费者问题,又有界缓冲区问题.两个进程共享一个一个公共的固定大小的缓冲区.其中一个是生产者,将信息放入缓冲区,另一个是消费者,从缓冲区中取信息. 问题的关键在于缓冲区已满,而此时生产者还想往其中 ...

  5. Linux线程编程之生产者消费者问题

    前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注意的事项.文中涉及的代码运行环境如下: 本文假定读者已具备线程同步的基础知识. 一  顺序表循环队列 1.1 ...

  6. Linux C多线程实现生产者消费者

    今天学习了用Linux C进行线程的同步,实现类似生产者消费者的问题.下面我就来分享我的代码 #include<stdio.h> #include<pthread.h> #in ...

  7. Linux线程编程之生产者消费者问题【转】

    转自:http://www.cnblogs.com/clover-toeic/p/4029269.html 前言 本文基于顺序循环队列,给出Linux生产者/消费者问题的多线程示例,并讨论编程时需要注 ...

  8. Linux组件封装(五)一个生产者消费者问题示例

    生产者消费者问题是计算机中一类重要的模型,主要描述的是:生产者往缓冲区中放入产品.消费者取走产品.生产者和消费者指的可以是线程也可以是进程. 生产者消费者问题的难点在于: 为了缓冲区数据的安全性,一次 ...

  9. Linux多线程实践(8) --Posix条件变量解决生产者消费者问题

    Posix条件变量 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr); int pthread_co ...

随机推荐

  1. Linux必学的命令

    Linux必学的命令Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正 ...

  2. PC-如何提高计算机的启动和关机的速度?

    如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...

  3. 几乎每个文件里面都有 #ifdef __cplusplus extern "C" { #endif 可我没找到程序里那个地方定义了__cplusplus 啊?这又是怎么回事呢?

    我们的C语言有个进化版,叫C++,这个想必楼主知道,Keil MDK是支持C++编程的,也就是说,你可以用C语言或者C++写你的程序,都可以. 但是,有一个问题,就是头文件的问题,C语言写的头文件C+ ...

  4. hdoj 3549 Flow Problem【网络流最大流入门】

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  5. 如何用jQuery实现在鼠标滚动后导航栏保持固定

    要实现如下效果,鼠标滚动后,上方导航栏置顶固定 关键html代码: <div class="header-bottom"> <div class="co ...

  6. SQLite 入门教程(二)创建、修改、删除表 (转)

    转于 SQLite 入门教程(二)创建.修改.删除表 一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 ...

  7. Identity-第一章

    本篇文章内容搭建Identity项目,实现几个用户基本的功能,了解Identity具体是什么. 一.Identity入门 Identity是微软在ASP.NET应用程序中管理用户的一个新的API. 1 ...

  8. Hyper-V虚机跨NUMA节点性能影响

    查看单个NUMA节点的内存大小: Hyper-V VM VID NUMA节点\PageCount:83769708376970*4k/1024/1024=32GB Hyper-V VM VID NUM ...

  9. Win7 64bit 安装VisualSVN出现报错:Servic 'VisualSVN Server' failed to start.解决办法

    问题描述: Win7 64bit 安装VisualSVN时出现报错: Servic 'VisualSVN Server' failed to start.Please check VisualSVN ...

  10. Apache Mina 2.x 框架+源码分析

    源码下载 http://www.apache.org/dyn/closer.cgi/mina/mina/2.0.9/apache-mina-2.0.9-src.tar.gz 整体架构 核心过程(IoA ...