#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<queue>
using namespace std;
#define LEN 128
typedef struct task_que
{
queue<char*> string_queue;
int m_flag ; //结束标志
int m_capacity;
pthread_mutex_t m_lock;
pthread_cond_t m_pro , m_con;
}QUE , *pQUE;
void* thd_func(void* arg);
void put(pQUE pq, char* src);
void get(pQUE pq, char* dest);
int main(int argc, char *argv[])
{
QUE aque;
aque.m_flag = ;
int nthds = atoi( argv[] ); //线程的个数
aque.m_capacity = atoi( argv[] );//字符串队列的大小
pthread_mutex_init( &aque.m_lock , NULL ); //锁
pthread_cond_init( &aque.m_pro ,NULL ); //生产者条件变量
pthread_cond_init( &aque.m_con , NULL ); //消费者条件变量
//开辟线程空间
pthread_t *thd_arr = ( pthread_t*)calloc(nthds, sizeof(pthread_t));
int* ntasks = (int*)calloc(nthds, sizeof(int));//用来记录 线程工作了几次
//创建线程
int index;
for(index = ; index < nthds; index ++)
{
pthread_create( thd_arr + index, NULL, thd_func,(void*)&aque );
} //输入字符串到队列中
char buf[LEN] ;
while( memset(buf, , LEN), fgets(buf, LEN, stdin) != NULL)
{
put(&aque, buf);
} //发出结束字符串
strcpy(buf, "over");
put(&aque, buf); for(index = ; index < nthds; index ++)
{
pthread_join(thd_arr[index], (void**)(ntasks + index ));
}
for(index = ; index < nthds; index ++)
{
printf("%d ", ntasks[index]);
}
printf("\n"); pthread_mutex_destroy(&aque.m_lock);
pthread_cond_destroy(&aque.m_pro);
pthread_cond_destroy(&aque.m_con);
return ;
}
void put(pQUE pq, char* src) //把字符串写到队列中
{
pthread_mutex_lock(&pq ->m_lock); //加锁
while(pq ->string_queue.size() == pq ->m_capacity) //队列满则阻塞
{
pthread_cond_wait(&pq -> m_pro, &pq ->m_lock); }
//插入队列
char* tem = ( char*)calloc( LEN , sizeof( char ));
strcpy(tem,src);
pq->string_queue.push(tem);
pthread_mutex_unlock(&pq -> m_lock); //解锁
pthread_cond_broadcast(&pq ->m_con); //唤醒所有消费者线程 } void get(pQUE pq, char* dest)
{
pthread_mutex_lock(&pq ->m_lock); //加锁
while(pq ->m_flag == && pq ->string_queue.empty() ) //队列空 并且未结束 则阻塞
{
pthread_cond_wait(&pq ->m_con, &pq ->m_lock);
}
if(pq ->m_flag == ) //判断结束标志
{
pthread_mutex_unlock(&pq ->m_lock); //解锁
return ;
}
//出队
strcpy(dest, pq ->string_queue.front());
pq->string_queue.pop(); pthread_mutex_unlock(&pq ->m_lock);
pthread_cond_signal(&pq ->m_pro); } void* thd_func(void* arg)
{
pQUE pq = (pQUE)arg ;
char buf[LEN] ;
int ncnt = ;
while()
{
memset(buf, , LEN) ;
get(pq, buf);
if(pq ->m_flag == ) //判断结束标志
{
printf("%u exit!\n", pthread_self());
pthread_exit((void*)ncnt); //退出
}
ncnt ++ ;
printf("%u: %s\n", pthread_self(), buf); //打印字符串
if(strcmp("over", buf) == ) //判断结束字符串
{
pq ->m_flag = ; //把结束符号置为 1
pthread_cond_broadcast(&pq ->m_con); //唤醒所有线程
pthread_exit((void*)ncnt); //退出
}
if(ncnt & == ) sleep(); //简单的负载平衡
}
}

1 producer — n consumers 模型 实现的更多相关文章

  1. 6张图为你分析Kafka Producer 消息缓存模型

    摘要:发送消息的时候, 当Broker挂掉了,消息体还能写入到消息缓存中吗? 本文分享自华为云社区<图解Kafka Producer 消息缓存模型>,作者:石臻臻的杂货铺. 在阅读本文之前 ...

  2. kafka consumer重复消费问题

    在做分布式编译的时候,每一个worker都有一个consumer,适用的kafka+zookeep的配置都是默认的配置,在消息比较少的情况下,每一个consumer都能均匀得到互不相同的消息,但是当消 ...

  3. python--线程知识详解

    Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. 1.1.threading模块 threading模块建立在_thread模块之上.thread模块以低级=原始的方式来处理 ...

  4. java 中多线程之间的通讯之等待唤醒机制

    wait notify () nitifyAll () 都使用在同步中,因为要对持有监视器(锁)的线程操作 所以要使用在同步中,因为只有同步才具有锁 为什么这些操作线程的方法要定义object类中呢 ...

  5. 关于LockSupport

    concurrent包的基础 Doug Lea 的神作concurrent包是基于AQS (AbstractQueuedSynchronizer)框架,AQS框架借助于两个类:Unsafe(提供CAS ...

  6. 二十二、Hadoop学记笔记————Kafka 基础实战 :消费者和生产者实例

    kafka的客户端也支持其他语言,这里主要介绍python和java的实现,这两门语言比较主流和热门 图中有四个分区,每个图形对应一个consumer,任意一对一即可 获取topic的分区数,每个分区 ...

  7. IBM developer:Kafka ACLs

    Overview In Apache Kafka, the security feature is supported from version 0.9. When Kerberos is enabl ...

  8. Kafka Frequently Asked Questions

    This is intended to be an easy to understand FAQ on the topic of Kafka. One part is for beginners, o ...

  9. Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)

    I wrote a blog post about how LinkedIn uses Apache Kafka as a central publish-subscribe log for inte ...

随机推荐

  1. MyBatis中时间格式的映射问题

    简单地说,就是Java的Date类可以直接映射到Mysql的TIMESTAMP或者是DATETIME(按道理应该是映射成DATE的) 具体的看这两篇博客吧: 1. MySql中TIMESTAMP和DA ...

  2. jQuery val()方法及valHooks源码解读

    val: function( value ) { var hooks, ret, isFunction, elem = this[0]; if ( !arguments.length ) {//无参数 ...

  3. IIS发布MVC应用程序问题

    1.IIS7.5详细错误 HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效 重复定义了“system.web.extensi ...

  4. java transient关键字作用,使用场景

    transient的作用及使用方法,官方解释为: Variables may be marked transient to indicate that they are not part of the ...

  5. Symbol.iterator 和 for of

    Symbol.iterator 和 for of 是es6的新特性 可以为对象设置 自己的迭代器 首先介绍我们的for of var arr = [1,2,3,8,33] for (var i of ...

  6. [BZOJ2982]combination Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982 $C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\ ...

  7. WPF MATERAIL DESIGN TOOKIT

    最近有个程序需要用到WPF,本以为WEB可以做到很炫的,没有想到XAML也能做到如此之炫.心动了,那就行动吧. 搜到有各种款式的:METRO风格,RIBBON风格,MATERIAL风格…… 也许是玩W ...

  8. How to install Eclipse in linux

    http://askubuntu.com/questions/26632/how-to-install-eclipse

  9. Adding other views to UIButton

    Q: I want to add some views to UIButton, for example multiple UILabels, UIImages etc. One I add thos ...

  10. (七)mybatis之创建SqlSession

    前文提到了SqlSessionFactory接口,可以用来生产SqlSession.而SqlSession其实也是一个接口类,也就是起到一个门面的角色.那么谁是真正干活的呢?------Executo ...