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. linuxmint获取root

    1.进入系统à点击桌面左下角的菜单à点击系统设置 2. 在系统设置里面找到登陆窗口并进去 3.打入自己设置的开机登陆密码 4. 选择选项,并把运行root登陆的勾打上 5.重启生效

  2. JBPM数据库表说明

    http://blog.163.com/tigerlion@126/blog/static/167675665201072642627400/ 最近这几天一直在研究JBPM工作流引擎,以下为JBMP的 ...

  3. CSS3 keyframes动画实现弹跳效果

    首先,“回到顶部”.“用户反馈”这两个按钮是通过定位放在左下角上. (1)“回到顶部”的按钮只有当滚动条有出现下滑时才出现 (2)“用户反馈”按钮,用户刚打开时会抖动一下,引起用户的注意,然后才定住. ...

  4. Kinect开发学习笔记之(一)Kinect介绍和应用

    Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.Kinect简单介绍 Kinectfor Xbox ...

  5. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  6. android104 帧动画,补间动画,属性动画

    ##帧动画FrameAnimation* 多张图片快速切换,形成动画效果* 帧动画使用xml定义 package com.itheima.frameanimation; import android. ...

  7. Linux curl命令参数详解--转载

    linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态. 一.Linux ...

  8. hdu1715(Java)大数相加

    大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  9. Android(java)学习笔记164:Relativelayout相对布局案例

    我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...

  10. DW一些快捷键的使用

    在键盘上敲空格的话可以使用shift+空格 如果要换行的话就可以使用的是 shift+enter