VC的生产者消费者模型
来源:互联网 发布:mac 删除智能文件夹 编辑:程序博客网 时间:2024/06/03 01:31
#include <windows.h>#include <stdio.h>#include <iostream>HANDLE empty_Semaphore;//设置信号量用。empty_Semaphore表示空的缓冲池的数量HANDLE full_Semaphore;//用full_Semaphore表示满的缓冲池的数量HANDLE mutex_Semaphore;//用mutex_Semaphore表示互斥信号量void Producer(void )//创建生产者进程{while(true){for (int i=1;i<10;i++){WaitForSingleObject(empty_Semaphore,-1);//对empty_Semaphore进行P操作WaitForSingleObject(mutex_Semaphore,-1);//对mutex_Semaphore进行P操作printf("生产者%d准备生产 \n",i);printf("生产者%d开始往缓冲区中写数据... \n",i);//生产者生产产品printf("生产者%d开始退出缓冲区... \n\n",i);ReleaseSemaphore(mutex_Semaphore,1,NULL);//对mutex_Semaphore进行V操作ReleaseSemaphore(full_Semaphore,1,NULL);//对full_Semaphore进行V操作} Sleep(1000);}return ;}void Consumer(void )//创建消费者进程{while(true){for (int j=1;j<10;j++){WaitForSingleObject(full_Semaphore,-1);//对full_Semaphore进行P操作WaitForSingleObject(mutex_Semaphore,-1); //对mutex_Semaphore进行P操作printf("消费者%d准备消费 \n",j);printf("消费者%d开始消费缓冲区中数据\n",j);//消费者消费产printf("消费者%d开始开始退出缓冲区... \n\n",j);ReleaseSemaphore(mutex_Semaphore,1,NULL);//对mutex_Semaphore进行V操作ReleaseSemaphore(empty_Semaphore,1,NULL);//对empty_Semaphore进行V操作}Sleep(1000);}return;}void main (){HANDLE Data[2];//创建HANDLE数组Dataempty_Semaphore=CreateSemaphore(NULL,10,10,NULL);//创建信号量empty_Semaphorefull_Semaphore=CreateSemaphore(NULL,0,10,NULL);//创建信号量full_Semaphoremutex_Semaphore=CreateSemaphore(NULL,1,1,NULL);//创建互斥信号量mutex_SemaphoreHANDLE handle[2];//创建HANDLE数组handle//以下是创建生产着和消费者进程handle[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Producer),&Data[0],0,NULL);if (handle[0]==NULL){printf("创建生产者线程失败 \n");}handle[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Consumer),&Data[1],0,NULL);if (handle[0]==NULL){printf("创建消费者线程失败 \n");}WaitForMultipleObjects(2,handle,TRUE,-1);//主进程等待线程的结束return;}
#ifndef CIRCULAR_BUFFER_H#define CIRCULAR_BUFFER_H#include <iostream>#include <vector>#include <windows.h>#include "CurrentPrice.h"class CircularBuffer{public:CircularBuffer(int capacity);~CircularBuffer();public:void push_back(const CurrentPrice& currentPrice);CurrentPrice pop_front();bool empty();private:HANDLE m_mutexSemaphore;HANDLE m_fullSemaphore, m_emptySemaphore;int m_begin, m_end, m_size;int m_capacity;std::vector<CurrentPrice> m_currentPrices;};#endif
#include "stdafx.h"#include "CircularBuffer.h"CircularBuffer::CircularBuffer(int capacity): m_begin(0), m_end(0), m_size(0), m_capacity(capacity), m_currentPrices(capacity){m_emptySemaphore=CreateSemaphore(NULL,capacity,capacity,NULL);//创建信号量empty_Semaphorem_fullSemaphore=CreateSemaphore(NULL,0,capacity,NULL);//创建信号量full_Semaphorem_mutexSemaphore=CreateSemaphore(NULL,1,1,NULL);//创建互斥信号量mutex_Semaphore}CircularBuffer::~CircularBuffer(){}void CircularBuffer::push_back(const CurrentPrice& currentPrice){WaitForSingleObject(m_emptySemaphore,-1);WaitForSingleObject(m_mutexSemaphore,-1);m_currentPrices[m_end] = currentPrice;m_end = (m_end+1) % m_capacity;++m_size;ReleaseSemaphore(m_mutexSemaphore, 1, NULL);ReleaseSemaphore(m_fullSemaphore,1,NULL);}CurrentPrice CircularBuffer::pop_front(){WaitForSingleObject(m_fullSemaphore,-1);WaitForSingleObject(m_mutexSemaphore,-1);CurrentPrice currentPrice = m_currentPrices[m_begin];//.front();m_begin = (m_begin+1) % m_capacity;--m_size;ReleaseSemaphore(m_mutexSemaphore, 1, NULL);ReleaseSemaphore(m_emptySemaphore,1,NULL);return currentPrice;}bool CircularBuffer::empty(){return m_currentPrices.empty();}
#ifndef CURRENT_PRICE#define CURRENT_PRICE#include <time.h>class CurrentPrice{public:CurrentPrice();~CurrentPrice();public:double get_price();void set_price(double price);tm get_current_time();void set_current_time(tm currentTime);private:double m_price;tm m_currentTime;};#endif
#include "stdafx.h"#include "CurrentPrice.h"CurrentPrice::CurrentPrice(){}CurrentPrice::~CurrentPrice(){}double CurrentPrice::get_price(){return m_price;}void CurrentPrice::set_price(double price){m_price = price;}tm CurrentPrice::get_current_time(){return m_currentTime;}void CurrentPrice::set_current_time(tm currentTime){m_currentTime = currentTime;}
0 0
- VC的生产者消费者模型
- 真实的生产者-消费者模型
- 生产者与消费者的模型
- 生产者消费者模型的实现
- 简单的生产者消费者模型
- 生产者消费者模型的演变
- 线程的生产者消费者模型
- 生产者消费者模型的实现
- Linux下的生产者消费者模型模型
- 并发模型的生产者消费者模型
- 消费者&生产者模型的python代码
- 关于生产者与消费者模型的概念
- 生产者-消费者模型的Java实现
- 3.4生产者/消费者的线程同步模型
- 对于生产者消费者模型的理解
- 生产者-消费者模型的Java实现
- 生产者-消费者模型的java实现
- [多线程] 生产者消费者模型的BOOST实现
- 利用python和VirusTotal的API接口写的在线样本扫描的脚本
- Android 下使用 Http 协议实现多线程断点续传下载
- exe调用dll中的对话框防资源冲突解决之道
- Spring是如何缓存单例bean
- jquery操作节点;jquery遍历节点
- VC的生产者消费者模型
- FMS Flex 视频应用开发
- Hive CSV Support(csv-serde)
- 第十五周-(项目三)带姓名的成绩单。
- 网页中返回顶部代码(多种方法)另附注释说明
- Mysql常用函数
- 指针篇之五 指针与数组 剪不断理还乱
- 爱情之线
- ifconfig使用及其配置文件