| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 206 人关注过本帖
标题:[求助]VC++中用对象解决同步问题?
只看楼主 加入收藏
baozishi
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-10-13
收藏
 问题点数:0 回复次数:0 
[求助]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
搜索更多相关主题的帖子: 对象 
2007-11-03 13:06
快速回复:[求助]VC++中用对象解决同步问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025563 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved