为什么当注释"cout"时此代码不起作用?
Why this code doesn't work when "cout"s are commented?
我正在基于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
的情况下进行表情,看看会发生什么,然后您就知道问题的位置。
相关文章:
- 为什么这段代码不起作用,我该如何解决?
- 为什么这些完全相似的代码不起作用?
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 注释一行使代码工作,而没有它,代码不起作用
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- C ++函数重新定义(代码不起作用 - 逻辑错误)
- 试图找到一个数字的平方根,但代码不起作用。C++
- 字符串代码不起作用
- 我正在努力在随机数组中查找最小值,有人知道为什么我的代码不起作用?C++
- 从 C++ 转换的 C# 代码不起作用
- 有人可以解释我,为什么我的代码不起作用吗?
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 无法在控制面板中创建轨道栏.示例代码不起作用
- CUDA我的共享内存代码不起作用,我缺少什么
- 为什么 C++ 中的 shell 脚本代码不起作用?
- 为什么C++代码不起作用(strncpy_s)?
- 模板函数声明为 void - 代码不起作用?
- 无法弄清楚为什么这个Arduino代码不起作用?
- "check if member exists using enable_if"中修改的代码不起作用