经典:生产者消费者模型
来源:互联网 发布:如何注册淘宝达人 编辑:程序博客网 时间:2024/06/07 04:52
首先是用信号量实现的
#include <stdio.h>#include <pthread.h>#include <time.h>#include <string.h>#include <semaphore.h>//信号量和缓冲区struct data{sem_t producer; //用来控制生产者,只有缓冲区为空,生产者才可以生产消息sem_t customer; //用来控制消费者,只有缓冲区有数据,才可以消费char buf[32]; //缓冲区};struct data model;void* producer_do(void *a){char *buf[] = {"香蕉","菠萝","橙子","西瓜","苹果","黄瓜","橘子"};while(1){int time = rand() % 100 + 1;usleep(time* 10000);sem_wait(&model.producer); // pstrcpy(model.buf,buf[rand()%7]);printf("生产的一个水果:%s\n",model.buf);sem_post(&model.customer); // v}}void* customer_do(void *a){while(1){int time = rand() % 100 + 1;usleep(time* 10000);sem_wait(&model.customer); // pprintf("吃了一个水果:%s\n",model.buf);sem_post(&model.producer); // v}}int main(){srand((unsigned int)time(NULL));//信号量初始化sem_init(&model.producer,0,1);sem_init(&model.customer,0,0);pthread_t producer_id;pthread_t customer_id;//生产者线程pthread_create(&producer_id,NULL,producer_do,NULL);//消费者线程pthread_create(&customer_id,NULL,customer_do,NULL);//等待线程pthread_join(producer_id,NULL);pthread_join(customer_id,NULL);//销毁信号量sem_destroy(&model.producer);sem_destroy(&model.customer);return 0;}
然后再加上互斥锁的
#include <stdio.h>#include <pthread.h>#include <time.h>//#include <string.h>#include <semaphore.h>#include "SqQuenue.h"//声明信号量struct data{sem_t producer;sem_t customer;QUEUE q;};//互斥锁pthread_mutex_t mutex;//创建信号量struct data model;//创建计数Queue_data num = 0;//生产者线程void* producer_do(void *a){while(1){int time = rand() % 100 + 1;usleep(time* 10000);sem_wait(&model.producer); // ppthread_mutex_lock(&mutex); //开锁num++;PushQueue(&(model.q),num);printf("生产了一个编号为%d的水果\n",num);pthread_mutex_unlock(&mutex); //解锁sem_post(&model.customer); // v}}//消费者线程void* customer_do(void *a){while(1){int time = rand() % 100 + 1;usleep(time* 10000);sem_wait(&model.customer); // ppthread_mutex_lock(&mutex); //开锁Queue_data x;PopQueue(&(model.q),&x);printf("消费了一个编号为%d的水果\n",x);pthread_mutex_unlock(&mutex); //解锁sem_post(&model.producer); // v}}int main(){srand((unsigned int)time(NULL));//信号量初始化sem_init(&model.producer,0,10);sem_init(&model.customer,0,0);//初始化互斥锁pthread_mutex_init(&mutex, NULL);//初始化队列Initqueue(&(model.q));pthread_t producer_id;pthread_t customer_id;//生产者线程pthread_create(&producer_id,NULL,producer_do,NULL);//消费者线程pthread_create(&customer_id,NULL,customer_do,NULL);//等待线程pthread_join(producer_id,NULL);pthread_join(customer_id,NULL);//销毁信号量sem_destroy(&model.producer);sem_destroy(&model.customer);//销毁互斥锁pthread_mutex_destroy(&mutex); return 0;}
阅读全文
1 0
- 经典:生产者消费者模型
- 经典软件设计模型 - 生产者消费者模型
- 线程同步之经典生产者-消费者模型
- JS实现经典生产者消费者模型
- 经典的生产者消费者模型(一)
- 经典的生产者消费者模型(二)
- 生产者/消费者模式(阻塞队列) 一个经典的并发模型
- 经典的消费者生产者模型——基于BlockingQueue
- 生产者/消费者模式(阻塞队列) 一个经典的并发模型
- 生产者/消费者模式(阻塞队列) 一个经典的并发模型
- 经典生产者消费者问题
- 【经典问题】生产者消费者
- 经典消费者生产者问题
- 生产者-消费者模型
- 生产者/消费者模型
- 生产者 消费者模型
- 生产者消费者模型
- 生产者&&消费者模型
- EasyDSS高性能流媒体服务器前端重构(一):从零开始搭建 webpack + vue + AdminLTE 多页面脚手架
- 解决Ubuntu14.04下声音突然没有问题
- It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by t
- 细说mybatis动态sql和底层数据交互与JDBC和hibernate的相关性
- 物料主数据--1. BAPI_MATERIAL_MAINTAINDATA_RT的使用以及心得
- 经典:生产者消费者模型
- vuex2.0 基本使用 --- mutation 和 action
- oracle中关于表空间,用户,表相关的查询语句
- 全国shp矢量数据分享(精确到区县)
- 知识点总结
- MySQL 密码忘记
- JQuery样式
- redis必杀命令:脚本
- Sql 各种查询