⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader_writer.cpp

📁 操作系统中,读者写者的问题
💻 CPP
字号:
#include <iostream.h>
#include <windows.h> 
#define THREAD_INSTANCE_NUMBER	4  //1,2为写线程,3,4为读线程
int iCounter=0;
HANDLE hMutex;

int SubThreadA(void * pD)
{
	int iID=(int)pD;
		//在内部打开
	HANDLE hCounterIn=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"sam sp 43");

	for(int i=0;i<3;i++)
	{   
		if (i==0||i==1) cout << "Write " << iID << " waiting..." << endl;
		else if(i==2||i==3) cout << "Read " << iID << " waiting..." << endl;
		WaitForSingleObject(hMutex,INFINITE);
		if(iCounter==0) 
        WaitForSingleObject(hCounterIn,INFINITE);
        iCounter++;
		ReleaseMutex(hMutex);
		if (i==0||i==1) cout << "Writeing " << iID << " (Database) access ..." << endl;
		else if(i==2||i==3) cout << "Reading " << iID << " (Database) access..." << endl;
        Sleep(500);
		if (i==0||i==1)cout << "Write " << iID << " (Database) has finished." << endl;
        else if(i==2||i==3)cout << "Read " <<iID << " (Database) has finished." <<endl;
        WaitForSingleObject(hMutex,INFINITE);
        iCounter--;
		if(iCounter==0)
        ReleaseSemaphore(hCounterIn,1,NULL);
        ReleaseMutex(hMutex);
		Sleep(500);
	}
	CloseHandle(hCounterIn);
	return 0;
}

int SubThreadB(void * pD)
{
	int iID=(int)pD;
		//在内部重新打开
	HANDLE hCounterIn=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"sam sp 43");

	for(int i=0;i<3;i++)
	{
		if(i==0||i==1)cout << "Write " << iID << " waiting..." << endl;
		else if (i==2||i==3)cout << "Read " << iID << " waiting..." << endl;
		WaitForSingleObject(hCounterIn,INFINITE);
		if (i==0||i==1)cout << "Writing " << iID << " (Database) access now..." << endl;
		else if (i==2||i==3)cout << "Reading " << iID << " (Database) access now ..." << endl;
		Sleep(500);
		if (i==0||i==1)cout << "Write" << iID << " (Database) has finished." << endl;
		else if(i==2||i==3)cout << "Read" << iID << " (Database) has finished." << endl;
		ReleaseSemaphore(hCounterIn,1,NULL);
	}
	CloseHandle(hCounterIn);
	return 0;
}

int main()
{
    int time;
    //创建信号灯 
	HANDLE hCounter=NULL;
	if( (hCounter=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"sam sp 43"))==NULL)
	{
		//如果没有其他进程创建这个信号灯,则重新创建
		hCounter = CreateSemaphore(NULL,1/*initial count*/,1/*maximum count*/,"sam sp 43");
	}
	// Create a thread;
    cout << "CreateThread" << endl;

	DWORD IDThread[THREAD_INSTANCE_NUMBER]; 
   	HANDLE hThread[THREAD_INSTANCE_NUMBER];
	int i;

	hMutex=CreateMutex(NULL,FALSE,"rmutex");
	//创建线程
	for (i=0;i<2;i++)
	{
		hThread[i] = CreateThread(NULL, // no security attributes 
	         0,                           // use default stack size 
	         (LPTHREAD_START_ROUTINE) SubThreadB, // thread function 
	         (void *)i,                    // no thread function argument 
	         0,                       // use default creation flags 
	         &(IDThread[i]));              // returns thread identifier 
				// Check the return value for success. 
		if (hThread[i] == NULL)
			cout << "CreateThread(Write) Faile!" << i << endl;
			else
			cout << "CreateThread:(Write) " << i << endl;
	
}
	for (i=2;i<4;i++)
	{
		hThread[i] = CreateThread(NULL, // no security attributes 
	         0,                           // use default stack size 
	         (LPTHREAD_START_ROUTINE) SubThreadA, // thread function 
	         (void *)i,                    // no thread function argument 
	         0,                       // use default creation flags 
	         &(IDThread[i]));              // returns thread identifier 
		
		// Check the return value for success. 
		if (hThread[i] == NULL)
			cout << "CreateThread(Read) Faile!" << i << endl;
			else
			cout << "CreateThread:(Read) " << i << endl;
	
}
		//等待线程结束
		WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
		//关闭句柄
		CloseHandle(hCounter);

		cout << endl << endl << "The \b\bwindow will be closed within 60s...";
	    for (time=60;time>0;time--)
	    {   Sleep (1000);
	        if(time>9) cout << "\b\b\b\b\b\b" << time << "s..."   ;
	        else cout<<"\b\b\b\b\b\b "<<time<<"s...";
	      }  
return 0;
}

⌨️ 快捷键说明

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