[求助]VC++中用对象解决同步问题?
程序是正确的...但是里面有很多调用的是WINDOWS的程序,所以有点不懂...红色的应该是调用的WINDOW里面自带的对象,但不是很清楚.帮我分析一下哈.3X.....
#include <windows.h>
#include <conio.h>//Console (键盘和屏幕) 输出输入函数的头文件
#include <stdlib.h>
#include <fstream.h>//文件读写流
#include <stdio.h>
#include <time.h>
#define INTE_PER_SEC 500
#define MAX_THREAD_NUM 64
#define RIGHT_VERSION TRUE
#define WRONG_VERSION FALSE
CRITICAL_SECTION my_section;
struct ThreadInfo
{
int serial;
double delay;
};
/*volatile*/ int accnt1 = 0;
/*volatile*/ int accnt2 = 0;
/*volatile*/ int accnt;
void account( char* file,BOOL version);
void acc_right(void* p);
void acc_wrong(void* p);
////////////////////////////////////////////////////////
// main function
////////////////////////////////////////////////////////
int main( int agrc, char* argv[] )
{
char ch;
while ( TRUE )
{
// Clear screen
system( "cls" );//调用DOS下的清屏命令"cls"
// display prompt info
printf("*********************************************\n");
printf(" 1.Start the right version(with the using of CRITICAL_SECTION)\n");
printf(" 2.Start the wrong version(without any algorithm of synchronization)\n");
printf(" 3.Exit to Windows\n");
printf("*********************************************\n");
printf("Input your choice(1 、2 or 3): ");
// if the number inputed is error, retry!
do
{
ch = (char)_getch();
}while ( ch != '1' && ch != '2'&& ch != '3');
system ( "cls" );
if ( ch == '1')
account("ex3.dat",RIGHT_VERSION);//传递值为TRUE
else if (ch =='2')
account("ex3.dat",WRONG_VERSION);//传递值为FALSE,没有同步运算
else if ( ch == '3')
return 0;
printf("\nPress any key to finish this Program. \n");
_getch();
} //end while
} //end main
void account( char* file,BOOL version)
{
DWORD n_thread = 0;
DWORD thread_ID;
DWORD wait_for_all;
// Tread Object Array
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
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
// initialize the data
srand((unsigned)time(NULL));//设置一个时间变量种子
accnt1=0;
accnt2=0;
// Create all thread
if (version==RIGHT_VERSION)
{
InitializeCriticalSection(&my_section); //init critical section
for ( int i = 0; i < (int)(n_thread); i++)
{
// Create a thread
h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(acc_right), &thread_info[i], 0, &thread_ID);
} //end for
} else
{
for ( int i = 0; i < (int)(n_thread); i++)
{
// Create a thread
h_Thread[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)(acc_wrong), &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_right(void* p)
{
DWORD m_delay;
int m_serial;
int tmp1 , tmp2, rand_num;
int counter = 0;
//get info from para
m_serial = ((ThreadInfo*) (p)) -> serial;
m_delay = (DWORD) (((ThreadInfo*)(p)) -> delay*INTE_PER_SEC);
do
{
EnterCriticalSection(&my_section);
//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;
//critical_section end
LeaveCriticalSection(&my_section);
counter++;
printf("Now the random number is %d ; and accnt1+accnt2 = %05d\n\n",rand_num,accnt);
} while (counter<10);
printf("At last of thread %d accnt1+accnt2 = %05d\n",m_serial,accnt);
} //end acc_right
void acc_wrong(void* p)
{
DWORD m_delay;
int m_serial;
int tmp1 , tmp2, rand_num;
int counter = 0;
//get info from para
m_serial = ((ThreadInfo*) (p)) -> serial;
m_delay = (DWORD) (((ThreadInfo*)(p)) -> delay*INTE_PER_SEC);
do
{
//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;
//critical_section end
counter++;
printf("Now the random number is %d ; and accnt1+accnt2 = %05d\n\n",rand_num,accnt);
} while (counter<5);
printf("At last of thread %d accnt1+accnt2 = %05d\n",m_serial,accnt);
} //end acc_wrong