📄 ex3.cpp
字号:
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <fstream.h>
#include <stdio.h>
#define INTE_PER_SEC 1000
#define MAX_THREAD_NUM 64
struct ThreadInfo
{
int serial;
double delay;
};
CRITICAL_SECTION RP_Write;
/*volatile*/ int accnt1 = 0;
/*volatile*/ int accnt2 = 0;
/*volatile*/ int accnt;
// Tread Object Array
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
void account( char* file);
void acc(void* p);
////////////////////////////////////////////////////////
// main fuction
////////////////////////////////////////////////////////
int main( int agrc, char* argv[] )
{
char ch;
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')
account("ex3");
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
void account( char* file)
{
DWORD n_thread = 0;
DWORD thread_ID;
DWORD wait_for_all;
InitializeCriticalSection(&RP_Write); //init critical sectoin
ifstream inFile;
inFile.open(file); //open file
printf( "Now, We begin to read thread Information to thread_info array \n\n" );
while ( inFile )
{
// read every thread info
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread++].delay;
inFile.get();
} //end while
// Create all thread
for( int i = 0; i < (int)(n_thread); i++)
{
// Create a thread
h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(acc), &thread_info[i], 0, &thread_ID);
} //end for
// Create thread
// waiting all thread will been finished
wait_for_all = WaitForMultipleObjects(n_thread,h_Thread,TRUE, -1);
printf("All threads have finished Operating.\n");
}// end account
void acc(void* p)
{
DWORD m_delay;
int m_serial;
int tmp1 , tmp2, rand_num;
int counter = 0;
//get info froam para
m_serial = ((ThreadInfo*) (p)) -> serial - 1;
m_delay = (DWORD) (((ThreadInfo*)(p)) -> delay*INTE_PER_SEC);
do {
EnterCriticalSection( &RP_Write );
//begin critical_section
printf("I am thread %d , I am doing %05dth step\n",m_serial,counter);
tmp1 = accnt1;
tmp2 = accnt2;
rand_num = rand();
accnt1 = tmp1 - rand_num;
Sleep(m_delay);
accnt2 = tmp2 + rand_num;
accnt = accnt1 + accnt2;
printf("Now accnt1+accnt2 = %05d\n",accnt);
//critical_section end
LeaveCriticalSection( &RP_Write );
counter++;
} while ( (accnt == 0) && (counter<10));
} //end acc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -