我正在基于IOCP编写用于在线游戏的服务器,而核心代码处理游戏消息如下:
CMessage ret;
int now_roomnum = recv_msg->para1;
int now_playernum = recv_msg->para2;
/*if(true)
{
cout<<"Received Game Message: "<<endl;
cout<<"type2 = "<<recv_msg->type2;
cout<<" player_num = "<<now_playernum<<" msg= "<<recv_msg->msg<<endl;
cout<<endl;
}*/
if(recv_msg->type2 == MSG_GAME_OPERATION)
{
ret.type1 = MSG_GAME;
ret.type2 = MSG_GAME_OPERATION;
while(game_host[now_roomnum].Ready(now_playernum) == true)
{
;
}
//cout<<"Entered from "<<now_playernum<<endl;
game_host[now_roomnum].SetMessage(now_playernum, recv_msg->msg);
game_host[now_roomnum].SetReady(now_playernum, true);
game_host[now_roomnum].SetUsed(now_playernum, false);
while(true)
{
bool tmp = game_host[now_roomnum].AllReady();
if(tmp == true)
break;
}
//cout<<"AllReady from"<<now_playernum<<endl;
string all_msg = game_host[now_roomnum].GetAllMessage();
game_host[now_roomnum].SetUsed(now_playernum, true);
while(!game_host[now_roomnum].AllUsed())
{
;
}
//cout<<"AllUsed from "<<now_playernum<<endl;
EnterCriticalSection(&cs);
game_host[now_roomnum].ClearReady();
LeaveCriticalSection(&cs);
strcpy_s(ret.msg, all_msg.c_str());
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
}
return ret;
现在,问题是:在PC上,当所有cout
如上所述评论时,游戏一次冻结;但是当我取消评论时,服务器运行良好。
更重要的是,当我在笔记本电脑上运行服务器时,无论我是否评论cout
,一切都很好。我的笔记本电脑和PC之间的主要区别在于,我的笔记本电脑的操作系统是Windows 8.1,而PC是Windows 7。
我完全困惑。如果有人能告诉我该怎么做,那将有很大的帮助。谢谢!
看起来像一个多线程问题。
顺便说一句,我看到您在ClearReady
周围使用关键部分,但在测试AllReady
时没有使用。该呼叫也应该包装(或者更好地编写使用锁的LockedAllReady
)。
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
您的意思是ret.msg?如果msg是方法,则必须执行ret.msg();,是一个字段吗?
如果您有这么好的话,那么就像他们上面说的那样,可能是一个计时问题,请尝试在没有ret.msg
的情况下进行表情,看看会发生什么,然后您就知道问题的位置。