0

I'm writing a server for an online game based on IOCP, and the core codes handling game message is something like below:

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;

Now, the problem is: on a PC, when all cout are commented like above, the game freezes at once; but when I cancel the comments, the server works well.

What's more, when I run the server on my laptop, everything goes fine, no matter whether I comment the cout or not. The main difference between my laptop and PC is that my laptop's OS is Windows 8.1, while the PC is Windows 7.

I'm totally confused. It will be of great help if someone can tell me what to do. Thank you!

DarkZero
  • 2,259
  • 3
  • 25
  • 36
  • Usually when you see a strange problem like this where code seems to stop working when you modify unrelated code it is probably an issue with your threading or timing, and it would help if you step through your code with a debugger to find out exactly where it is freezing. Also, those `while(/*true*/);` loops should probably be replaced with `while(/*true*/) Sleep(1);` or something similar to give the OS an opportunity to process other threads. – Wernsey May 14 '15 at 05:41
  • 1
    @Wernsey Sleep(1) is almost certainly very bad advice – David Heffernan May 14 '15 at 06:27
  • @DavidHeffernan Probably true, but would you elaborate on what you would do to yield the current thread to the operating system? – Wernsey May 14 '15 at 06:40
  • @Wernsey It's a game loop. You might not want to yield. There is SwitchToThread and indeed many other options for yielding. – David Heffernan May 14 '15 at 06:50
  • @DavidHeffernan I've had my coffee now and seen the errors of my ways. – Wernsey May 14 '15 at 07:33
  • Thank you both, in fact I tried `Sleep(1)` and I discovered a race problem. I thought sleeping 1ms is too long, so I deleted `Sleep(1)`... And on my laptop it works well... Now I'm wondering if this is actually caused by race problem, and rewriting it with semaphores. – DarkZero May 14 '15 at 08:28

2 Answers2

0

Looks like a multithreading issue.

By the way I see you use a Critical section around ClearReady but not when testing for AllReady. That call should be wrapped as well (or, better, write a LockedAllReady that makes use of the lock).

George Netu
  • 2,758
  • 4
  • 28
  • 49
marom
  • 5,064
  • 10
  • 14
  • Thank you, in fact `AllReady` does the following thing (`AllUsed` is silimar): check if `ready[i]` is true for i=1 to 4. so it doesn't need to be wrapped in a critical section. – DarkZero May 14 '15 at 08:22
0
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;

What you mean by ret.msg? if msg is method you must do ret.msg(); , is it a field?

If you have this good then like they say above probably a timing problem, try to do cout without ret.msg and see what will happen, and then you know from where the problem is.

MaxDevelop
  • 637
  • 1
  • 4
  • 16
  • This is a online game server, its duty is to collect four players' operations (described in ` char msg[100]` of `CMessage`) , concatenate them together and send it back to the client so that the clients will have all four players' operations during this frame. – DarkZero May 14 '15 at 08:21