📄 ex4.cpp
字号:
#include <windows.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>
#define MAX_THREAD_NUM 64
struct ThreadInfo
{
int serial;
char entity;
int from;
int to;
};
int prime[9];
int count = 0;
HANDLE h_Full;
HANDLE h_Empty;
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
int readcount=0;
int writecount=0;
int readThread=0;
int writeThread=0;
void Control_Thread( char* file );
void RP_ReaderThread(void* p);
void RP_WriterThread(void* p);
////////////////////////////////////////////////////////
// main fuction
////////////////////////////////////////////////////////
int main( int agrc, char* argv[] )
{
char ch;
for(int i =0;i < 9;i ++)
prime[i] = 0;
while ( TRUE )
{
// Cleare screen
system( "cls" );
// display prompt info
printf("*********************************************\n");
printf(" 1.Start test\n");
printf(" 2.Exit to Windows\n");
printf("*********************************************\n");
printf("Input your choice(1or2): ");
// if the number inputed is error, retry!
do{
ch = (char)_getch();
}while( ch != '1' && ch != '2');
system ( "cls" );
if ( ch == '1')
Control_Thread("ex4.dat");
else if ( ch == '2')
return 0;
printf("\nPress any key to finish this Program. \nThank you test this Proggram!\n");
_getch();
} //end while
} //end main
///////////////////////////////////////////////////////////////
// Reader Priority fuction
// file: filename
//////////////////////////////////////////////////////////////
void Control_Thread( char* file )
{
DWORD n_thread = 0;
DWORD thread_ID;
DWORD wait_for_all;
h_Full = CreateEvent( NULL,TRUE,TRUE,"event_for_full" );
h_Empty = CreateEvent( NULL,TRUE,TRUE,"event_for_empty" );
ifstream inFile;
inFile.open(file); //open file
printf( "Control Thread:\n\n" );
while ( inFile )
{
// read every reader/writer info
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].from;
inFile>>thread_info[n_thread++].to;
inFile.get();
} //end while
for( int i = 0; i < (int)(n_thread); i++)
{
if(thread_info[i].entity == 'D' || thread_info[1].entity == 'd')
{
// Create Reader thread
h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(RP_ReaderThread), &thread_info[i], 0, &thread_ID);
readThread++;
}
else {
// Create Writer thread
h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(RP_WriterThread), &thread_info[i], 0, &thread_ID);
writecount++;
}
} //end for
// waiting all thread will been finished
wait_for_all = WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("All reader and writer thread have finished Operating.\n");
}// end Control Thread
////////////////////////////////////
// p: reader thread info
////////////////////////////////////
void RP_ReaderThread(void* p)
{
h_Full = OpenEvent(MUTEX_ALL_ACCESS,FALSE,"event_for_full");
DWORD wait_for_full;
DWORD m_count;
int m_serial;
int i = 0;
//get info froam para
m_serial = ((ThreadInfo*) (p)) -> serial;
m_count = (DWORD) (((ThreadInfo*)(p)) -> from);
do
{
printf("Reader thread %d sents the reading require .\n",m_serial);
if(count==0&&writeThread==0)
{
break;
}
wait_for_full = WaitForSingleObject(h_Full,-1);
if(wait_for_full==WAIT_OBJECT_0)
{
while(!prime[i])
{
i = (i + 1)%9;
}
cout<<"read: "<<prime[i]<<" from "<<i<<endl;
prime[i] = 0;
count--;
readcount++;
SetEvent(h_Empty);
}
else return;
Sleep(50);
}while(readcount<(int)m_count);
if(readThread!=0) readThread--;
return;
}
////////////////////////////////////
// p: writer thread info
///////////////////////////////////
void RP_WriterThread(void* p)
{
h_Empty = OpenEvent(MUTEX_ALL_ACCESS,FALSE,"event_for_empty");
DWORD wait_for_empty;
DWORD m_from;
DWORD m_to;
int m_serial;
m_serial = ((ThreadInfo*) (p)) -> serial;
m_from = (DWORD) (((ThreadInfo*)(p)) -> from);
m_to = (DWORD) (((ThreadInfo*)(p)) -> to);
int i =0;
for(int j = (int)m_from;j < (int)m_to;j ++)
{
printf("Writer thread %d sents the writing require .\n",m_serial);
if(j == 1) continue;
for(int k = 2;k <= j/2;k ++)
{
if( j%k == 0)
break;
}
if(k < j/2 + 1)
continue;
if(count==9&&readThread==0) break;
wait_for_empty=WaitForSingleObject(h_Empty,-1);
if(wait_for_empty==WAIT_OBJECT_0)
{
while(prime[i])
{
i = (i + 1)%9;
}
prime[i] = j;
cout<<"write: "<<j<<" to "<<i<<endl;
count++;
SetEvent( h_Full );
}
Sleep(70);
}
if(writeThread!=0) writeThread--;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -