C++ 用critical_section 代碼臨界區模擬信號量,解決生產者消費者的問題

PS.不要問我爲什麼不直接用CRITICAL_SECTION,我只是突然腦洞大開,想自己寫個信號量的操作函數。

問題描述:

生產者消費者的問題描述了兩個共享固定大小的線程——即所謂的“生產者”和“消費者”——在實際運行時會發生的問題。生產者的主要作用是生成一定量的數據放到緩衝區中,然後重複此過程。與此同時,消費者也在緩衝區消耗這些數據。該問題的關鍵就是要保證生產者不會在緩衝區滿時加入數據,消費者也不會在緩衝區中空時消耗數據。


函數描述:

producer:

while(count==n) do no-op;//內存滿,不操作

wait(S);

produce an item into buffer...

in=(in+1)%n;

counter++;

signal(S);

consumer:

while(count==0) do no-op;

wait(S);

consume an item from buffer;

out=(out+1)%n;

count--;

signal(S);

實現目標,通過CRITICAL_SECTION 代碼臨界區模擬實現信號量的wait,和signal功能


wait(S)以及signal(S)函數:

在操作系統中,wait和signal是一對信號量的操作函數,他們都具有原子性。

操作描述:

	wait(S): 
	while(S<=0) do no-op;
	S:=S-1;

	signal(S):
	S:=S+1;

這兩個函數都是源自操作,因此,他們在執行是是不可中斷的。亦即,當一個函數在進程中修改某個信號量時,是沒有其他進程可同時對該信號量進行操作。此外,在wait函數中的對S的測試和等待是不可中斷的。


關於CRITICAL_SECTION這裏就不在多說,主要介紹幾個函數

CRITICAL_SECTION cs;
initializeCriticalSection(&cs);//初始化一個臨界資源
enterCriticalSection(&cs);//進入臨界資源
leaveCriticalSection(&cs);//退出臨界資源

直接上代碼

#include <iostream>
#include <windows.h>
using namespace std;


DWORD WINAPI producer(LPVOID lpParameter);//生產者的函數
DWORD WINAPI consumer(LPVOID lpParameter);//消費者函數
void waitSemaphore(int &semephore);
void releaseSemaphore(int &semaphore);

int n=10;
int in=0;
int out=0;
int *buffer=new int[n];//用數組來模擬資源
int counter=0;
CRITICAL_SECTION cs;
CRITICAL_SECTION cs1;
int mutex=1;
int empty=1;
int full=0;
void main()
{
	for(int i=0;i<n;i++)
	{
		buffer[i]=0;
	}
	InitializeCriticalSection(&cs);
	InitializeCriticalSection(&cs1);
	HANDLE Thread1;
	HANDLE Thread2;
	HANDLE Thread3;
	Thread1=CreateThread(NULL,0,producer,NULL,0,NULL);
	Thread2=CreateThread(NULL,0,consumer,NULL,0,NULL);
	Thread3=CreateThread(NULL,0,producer,NULL,0,NULL);

	Sleep(40000);

	CloseHandle(Thread1);
	CloseHandle(Thread2);
	CloseHandle(Thread3);
	DeleteCriticalSection(&cs);
	DeleteCriticalSection(&cs1);
}
void waitSemaphore(int &semaphore)
{
	EnterCriticalSection(&cs);
	while(semaphore==0)
	{
		NULL;
	}
	semaphore=0;
	LeaveCriticalSection(&cs);
}
void releaseSemaphore(int &semaphore)
{
	EnterCriticalSection(&cs1);
	semaphore=1;
	LeaveCriticalSection(&cs1);
}
DWORD WINAPI producer(LPVOID lpParameter)
{
	while(true)
	{
		
		while(counter==n)
		{
			NULL;
		}
		waitSemaphore(mutex);
		Sleep(3000);
		int nextp=rand()%10+1;
		buffer[in]=nextp;
		in=(in+1)%n;
		counter=counter+1;
		cout<<"生產:"<<endl;
		cout<<"counter:"<<counter<<endl;
		for(int i=0;i<n;i++)
		{
			cout<<buffer[i]<<" ";
		}
		cout<<endl;
		releaseSemaphore(mutex);
	}
	return 0;
}
DWORD WINAPI consumer(LPVOID lpParameter)
{
	while(true)
	{
		//waitSemaphore(full);
		while(counter==0)
		{
			NULL;
		}
		waitSemaphore(mutex);
		Sleep(3000);
		int nextc=buffer[out];
		buffer[out]=0;
		out=(out+1)%n;
		counter=counter-1;
		cout<<"消費:"<<endl;
		cout<<"counter:"<<counter<<endl;
		cout<<"netxc:"<<nextc<<endl;
		for(int i=0;i<n;i++)
		{
			cout<<buffer[i]<<" ";
		}
		cout<<endl;
		//releaseSemaphore(empty);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章