在EVC4下面,我做中国象棋游戏,使用了线程,并构造函数里面初始化,可是一旦编译完成后,模拟器上面的运行速度慢得吓人。不知道是什么原因。哪位大侠帮忙看看吧。
声明:CWinThread* m_pThinkThread;
在构造函数:m_pThinkThread=AfxBeginThread (_bogusthreadfunc,this,THREAD_PRIORITY_NORMAL);//把这句话屏蔽了,就恢复正常速度了
下面是几个关键的函数
UINT CThinker::ThinkProc() //思考线程
{
int i,cur,maxvalue[3],curvalue;
char *bman,*btox,*btoy;
int *pcount;
CMove maxmove[3];
const char strman[14][3]=
{"帅","仕","相","马","车","炮","兵","将","士","象","马","车","炮","卒"};
// TRACE("进入思考线程\n");
while(1)
{
cs.Lock();
if(m_bWaitForExit)
{
cs.Unlock();
goto _EXIT;
}
cs.Unlock();
cs.Lock();
if(m_bWaitForCut)
{
cs.Unlock();
goto _CUT;
}
cs.Unlock();
cs.Lock();
if(!m_bThinking)
{
cs.Unlock();
continue;
}
cs.Unlock();
curvalue=-10000;
maxvalue[0]=-10000;
maxvalue[1]=-10001;
maxvalue[2]=-10002;
cs.Lock();
tlevel = m_nLevel;
cs.Unlock();
tdeep =0;
bman=tman[0];
btox=ttox[0];
btoy=ttoy[0];
pcount=& tcount[0];
*pcount=0;
for(i=0;i<32;i++)
{
tmanx=m_FaceToThink.man.x;
tmany=m_FaceToThink.man.y;
}
tside=m_FaceToThink.side;
FixManMap(m_FaceToThink,tmap);
cur=0;
EnumList();
i=*pcount;
while(cur
{
curvalue=SubThink(bman[cur],btox[cur],btoy[cur]);
//---------------------------
bman=tman[0];
btox=ttox[0];
btoy=ttoy[0];
//----------------------------
//防止兑子过快:
if(tmap[btox[cur]][btoy[cur]]!=32) curvalue-=BV1[4]/18;
/*TRACE("%2d: %s(%2d,%2d) - (%2d,%2d) =%4d\n"
,cur
,strman[ManToType[bman[cur]]]
,tmanx[bman[cur]]
,tmany[bman[cur]]
,btox[cur]
,btoy[cur]
,curvalue);*/
if(curvalue >maxvalue[0])
{
maxmove[2]=maxmove[1]; maxvalue[2]=maxvalue[1];
maxmove[1]=maxmove[0]; maxvalue[1]=maxvalue[0];
maxmove[0].man=bman[cur];
maxmove[0].x=btox[cur];
maxmove[0].y=btoy[cur];
maxvalue[0]=curvalue;
}
else if(curvalue >maxvalue[1])
{
maxmove[2]=maxmove[1]; maxvalue[2]=maxvalue[1];
maxmove[1].man=bman[cur];
maxmove[1].x=btox[cur];
maxmove[1].y=btoy[cur];
maxvalue[1]=curvalue;
}
else if(curvalue >maxvalue[2])
{
maxmove[2].man=bman[cur];
maxmove[2].x=btox[cur];
maxmove[2].y=btoy[cur];
maxvalue[2]=curvalue;
}
cur ++;
cs.Lock();
m_nPercent=((cur+1)*100)/i;
cs.Unlock();
//----------这一段要保证随时能调用
cs.Lock();
if(m_bWaitForExit)
{
cs.Unlock();
goto _EXIT;
}
cs.Unlock();
cs.Lock();
if(m_bWaitForCut)
{
cs.Unlock();
goto _CUT;
}
cs.Unlock();
// --------------------------------
}
//结束一次计算:
cs.Lock();
float f;
f=(float(maxvalue[0]-maxvalue[2])/(float(maxvalue[0]+maxvalue[1]+maxvalue[2])/3));
if(f<0.1 && f>-0.1)
m_moveResult=maxmove[rnd(3)];
else
{
f=(float(maxvalue[0]-maxvalue[1])/(float(maxvalue[0]+maxvalue[1])/2));
if(f<0.1 && f>-0.1) m_moveResult=maxmove[rnd(2)];
else m_moveResult=maxmove[0];
}
/*TRACE("结果: %s(%2d,%2d) - (%2d,%2d)\n"
,strman[ManToType[m_moveResult.man]]
,m_FaceToThink.man[m_moveResult.man].x
,m_FaceToThink.man[m_moveResult.man].y
,m_moveResult.x
,m_moveResult.y);*/
m_bThinkOver=TRUE;
//-------------------------
for(i=0;i<32;i++)
{
m_FaceToThink.man.x=tmanx;
m_FaceToThink.man.y=tmany;
}
m_FaceToThink.side=tside;
//-------------------------------
cs.Unlock();
goto _DDD;
_CUT: cs.Lock();
m_Cut.SetEvent();
if(m_bWaitForCut)m_bWaitForCut=FALSE;
cs.Unlock();
_DDD: cs.Lock();
m_bThinking=FALSE;
cs.Unlock();
}
_EXIT: //TRACE("退出思考线程\n");
cs.Lock();
m_Stoped.SetEvent();
cs.Unlock();
return 0;
}
UINT CThinker::_bogusthreadfunc(LPVOID lpparam)
{
CThinker* This=(CThinker*)(lpparam);
return This->ThinkProc();
}
给大家推荐个
好的技术群 大家一起学习啊
32141688
如果线程不需要动作的话,最好让它处于空闲状态, 一般可用WaitForSingleObject等待事件发生,