applyarray.cpp

来自「模拟Linux文件系统 在任一OS下」· C++ 代码 · 共 74 行

CPP
74
字号
#include"disk.h"
#include <winbase.h>
#define _WIN32_WINNT 0x0400
#if(_WIN32_WINNT >= 0x0400)
WINBASEAPI
DWORD
WINAPI
SignalObjectAndWait(
HANDLE hObjectToSignal,
HANDLE hObjectToWaitOn,
DWORD dwMilliseconds,
BOOL bAlertable
);
#endif /* _WIN32_WINNT >= 0x0400 */  



CRITICAL_SECTION thereisapplication;
CRITICAL_SECTION thereisspace;
CRITICAL_SECTION   getLock;
CRITICAL_SECTION   putLock;
CRITICAL_SECTION   ArrayLock;
applyarray::applyarray(){
	beg=end=0;
}
void applyarray::able_get(){
	EnterCriticalSection (&getLock);
	if(beg==end)
		SignalObjectAndWait(&getLock,
                            &thereisapplication,
                            INFINITE, 
                            FALSE);
	LeaveCriticalSection (&getLock);
}
void applyarray::get()
{
	EnterCriticalSection (&ArrayLock);
	if(beg=end){
		cout<<"the thread of the keservicedispatchfunction is wrong"<<endl;
		return;
	}
	int k=array[beg];
	beg=(beg+1)%applyarraysize;
	LeaveCriticalSection (&ArrayLock);
	EnterCriticalSection (&putLock);
	if((end+2)%applyarraysize==beg)
		SetEvent(&thereisspace);
	LeaveCriticalSection (&putLock);
}
void applyarray::able_put(){
	EnterCriticalSection (&putLock);
	if((end+1)%applyarraysize==beg)
		::SignalObjectAndWait(&getLock,
                            &thereisspace,
                            INFINITE, 
                            FALSE);
	LeaveCriticalSection (&putLock);
}
void applyarray::put(int i)
{
	EnterCriticalSection (&ArrayLock);
	if((end+1)%applyarraysize==beg){
		cout<<"the thread of the user is wrong"<<endl;
		return;
	}
	array[end]=i;
	end=(end+1)%applyarraysize;
	LeaveCriticalSection (&ArrayLock);
	EnterCriticalSection (&getLock);
	if((beg+1)%applyarraysize==end)
		SetEvent(&thereisapplication);
	LeaveCriticalSection (&getLock);	
}
	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?