Posted by
| Nick Gammon
Australia (22,982 posts) bio
Forum Administrator |
Message
| I am pretty certain what your problem is here. :)
It is nothing to do with 64-bit numbers, dual-CPUs or anything like that.
Your main problem is you have two "select" calls in your main loop.
The first one is used to check for incoming IO on the sockets:
if( select( maxdesc + 1, &in_set, &out_set, &exc_set, &null_time ) < 0 )
The second is attempting to introduce some sort of delay into processing (your "game pulse"):
#ifdef WIN32
Sleep( ( stall_time.tv_sec * 1000L ) + ( stall_time.tv_usec / 1000L ) );
#else
if( select( 0, NULL, NULL, NULL, &stall_time ) < 0 && errno != EINTR )
Note, and this is important, that when I did the Windows version I found that the select here did nothing, and thus I did a Sleep instead, otherwise it just went crazy using CPU very quickly.
Check the documentation for select:
timeout is an upper bound on the amount of time elapsed before select returns.
Notice the words "upper bound". It does not guarantee that the time will actually elapse.
I suggest rewriting with only a single select in the game loop (my preferred option), or make the "time delay" select use a different method, like the Sleep in Windows.
Another approach would be to take the "real" select (the one that waits on sockets), introduce a small fixed delay (eg. 1/10 second), and then check (using the system clock) whether your pulse time is up.
Or, perhaps, make the real select wait 1/4 second, which is your game pulse time anyway, then your only problem is that the select might return sooner, because of TCP IO. Thus you still need a check, to not do a game pulse, if 1/4 second has not elapsed since last time through the loop. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|