关闭

生产者—消费者模式的两种同步实现

标签: 生产者消费者模式进程同步CriticalSectionMutex操作系统
811人阅读 评论(0) 收藏 举报
分类:

简要问题描述

一组生产者进程和一组消费者进程共享一个初始为空、大小为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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55728次
    • 积分:918
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:8篇
    • 译文:0篇
    • 评论:58条
    最新评论