生产者—消费者模式的两种同步实现
2016-06-01 19:45
706 查看
简要问题描述
一组生产者进程和一组消费者进程共享一个初始为空、大小为n的缓冲区,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必须等待;只有缓冲区不空时,消费者才能从中取出消息,否则必须等待。由于缓冲区是临界资源,它只允许一个生产者放入消息,或者一个消费者从中取出消息。
生产者—消费者模式是非常经典的进程同步问题,我目前具体的两个简单实现为临界区(CriticalSection)同步和互斥(Mutex)同步,具体两者之间有何区别,可以参考负责任的链接,hhh~
Version1:
Version2:
一组生产者进程和一组消费者进程共享一个初始为空、大小为n的缓冲区,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必须等待;只有缓冲区不空时,消费者才能从中取出消息,否则必须等待。由于缓冲区是临界资源,它只允许一个生产者放入消息,或者一个消费者从中取出消息。
生产者—消费者模式是非常经典的进程同步问题,我目前具体的两个简单实现为临界区(CriticalSection)同步和互斥(Mutex)同步,具体两者之间有何区别,可以参考负责任的链接,hhh~
Version1:
#include<windows.h> #include<iostream> #include<process.h> #include<stdlib.h> #include<time.h> using namespace std; const unsigned short Size = 10; // 缓冲区长度 unsigned short curValue = 0; // 当前产品数 bool g_continue = true; // 控制程序结束 :true(运行);false(结束) CRITICAL_SECTION g_cs; //临界区 HANDLE g_hFullSemaphore; //当缓冲区满时迫使生产者等待 HANDLE g_hEmptySemaphore; //当缓冲区空时迫使消费者等待 DWORD WINAPI Producer(LPVOID); //生产者线程 DWORD WINAPI Consumer(LPVOID); //消费者线程 int main() { InitializeCriticalSection(&g_cs); // 初始化临界区 g_hFullSemaphore = CreateSemaphore(NULL,Size-1,Size-1,NULL); g_hEmptySemaphore = CreateSemaphore(NULL,0,Size-1,NULL); //创建生产者线程 srand( (unsigned)time( NULL ) ); int t1 = rand()%(Size+1); for (int i = 0; i < t1; i++) { HANDLE producer = CreateThread(NULL,0,Producer,NULL,0,NULL); } //创建消费者线程 srand( (unsigned)time( NULL ) ); int t2 = rand()%(Size+1); for (int i = 0; i < t2; i++) { HANDLE consumer = CreateThread(NULL,0,Consumer,NULL,0,NULL); } while(g_continue){ if(getchar()) g_continue = false; //按回车后终止程序运行 } return 0; } void Produce() { if (curValue == 10) return; cout << "生产之前产品数为:" << curValue << endl; curValue++; cout << "Produce Succeed" << endl; cout << "当前产品数为:" << curValue << endl; cout << endl; } void Consume() { if (curValue == 0) return; cout << "消费之前产品数为:" << curValue << endl; curValue--; cout << "Consume Succeed" << endl; cout << "当前产品数为:" << curValue << endl; cout << endl; } //生产者 DWORD WINAPI Producer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hFullSemaphore,INFINITE); EnterCriticalSection(&g_cs); Produce(); Sleep(1000); LeaveCriticalSection(&g_cs); ReleaseSemaphore(g_hEmptySemaphore,1,NULL); } return 0; } //消费者 DWORD WINAPI Consumer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hEmptySemaphore,INFINITE); EnterCriticalSection(&g_cs); Consume(); Sleep(1000); LeaveCriticalSection(&g_cs); ReleaseSemaphore(g_hFullSemaphore,1,NULL); } return 0; }
Version2:
#include<windows.h> #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; const unsigned short Size = 10; // 缓冲区长度 unsigned short curValue = 0; // 当前产品数 bool g_continue = true; // 控制程序结束:true(运行);false(结束) HANDLE g_hMutex; //用于线程间的互斥 HANDLE g_hFullSemaphore; //当缓冲区满时迫使生产者等待 HANDLE g_hEmptySemaphore; //当缓冲区空时迫使消费者等待 DWORD WINAPI Producer(LPVOID); //生产者线程 DWORD WINAPI Consumer(LPVOID); //消费者线程 int main() { //创建各个互斥信号 g_hMutex = CreateMutex(NULL,FALSE,NULL); g_hFullSemaphore = CreateSemaphore(NULL,Size-1,Size-1,NULL); g_hEmptySemaphore = CreateSemaphore(NULL,0,Size-1,NULL); //创建生产者线程 srand( (unsigned)time( NULL ) ); int t1 = rand()%(Size+1); for (int i = 0; i < t1; i++) { HANDLE producer = CreateThread(NULL,0,Producer,NULL,0,NULL); } //创建消费者线程 srand( (unsigned)time( NULL ) ); int t2 = rand()%(Size+1); for (int i = 0; i < t2; i++) { HANDLE consumer = CreateThread(NULL,0,Consumer,NULL,0,NULL); } while(g_continue){ if(getchar()) g_continue = false; //按回车后终止程序运行 } return 0; } void Produce() { if (curValue == 10) return; cout << "生产之前产品数为:" << curValue << endl; curValue++; cout << "Produce Succeed" << endl; cout << "当前产品数为:" << curValue << endl; cout << endl; } void Consume() { if (curValue == 0) return; cout << "消费之前产品数为:" << curValue << endl; curValue--; cout << "Consume Succeed" << endl; cout << "当前产品数为:" << curValue << endl; cout << endl; } //生产者 DWORD WINAPI Producer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hFullSemaphore,INFINITE); WaitForSingleObject(g_hMutex,INFINITE); Produce(); Sleep(1000); ReleaseMutex(g_hMutex); ReleaseSemaphore(g_hEmptySemaphore,1,NULL); } return 0; } //消费者 DWORD WINAPI Consumer(LPVOID lpPara) { while(g_continue){ WaitForSingleObject(g_hEmptySemaphore,INFINITE); WaitForSingleObject(g_hMutex,INFINITE); Consume(); Sleep(1000); ReleaseMutex(g_hMutex); ReleaseSemaphore(g_hFullSemaphore,1,NULL); } return 0; }
相关文章推荐
- 应用领航:盘点那些年我们一起追过的OS
- 无奇不有!盘点各国自己开发的操作系统
- 可自定义oem的萝卜家园 Ghost XP 新春装机版 V200801 下载
- C#实现判断操作系统是否为Win8以上版本
- js获取本机操作系统类型的两种方法
- Linux操作系统添加新硬盘方法
- C#使用Mutex简单实现程序单实例运行的方法
- java如何获取本地操作系统进程列表
- Linux rdesktop操作系统下远程登录Windows XP桌面
- 32位操作系统认出超出4G内存的方法
- Linux rpm tar 操作系统下软件的安装与卸载方法
- JavaScript 获取用户客户端操作系统版本
- jsp 获取客户端的浏览器和操作系统信息
- Windows 操作系统的安全设置
- C#中的lock、Monitor、Mutex学习笔记
- php判断当前操作系统类型
- PHP获取用户的浏览器与操作系统信息的代码
- Perl操作系统环境变量的脚本代码
- javascript获取本机操作系统类型的方法
- 封装好的js判断操作系统与浏览器代码分享