Rare behavior. rendering<->logic sync howto

Posts: 93
Joined: 2008.11
Post: #1
Hello everybody,

I am working on a game where objects should move smooth but very fast across the screen. I have two threads, one for rendering and one for logic. My objects have two methods. One updates the logic and the other can be used to render the object. These methods are called from the two threads mentioned above.
Of course I'm forced to use a mutex so that my threads wont mess around. Most of the time things go pretty ok, however the whole system lags a lot. It looks like we are skipping some frames so the object just jumps from one place to another.
I use texture maps so I'm not loading a lot of different small textures for every object and thus not switching between textures that much. I wouldn't expect the bottleneck to be in opengl. To me it looks more like a synchronization issue or something like that. Maybe thread priorities should be set? I tried to play with them but it didn't do any better.

Anyway my question is: What is the usual/better way to handle synchronization between two threads provided that we are developing a game here? How do _you_ do/did this? How games like jelly car, quake, doom etc. get such smooth graphics (these games are MUCH more complicated than mine)?
Maybe someone can provide some code or point me to a _good_ example in a opensource game for the iPhone..

Thanks in advance!
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #2
Is there an actual compelling reason to use two threads? You could potentially save yourself a huge amount of headache by switching to a single-threaded paradigm, but I'm not well-versed enough in iPhone programming to know if there's some inherent benefit to multithreading for this that I'm missing. Since you only have one CPU core, it seems like it's likely to slow you down more than anything.
Quote this message in a reply
Posts: 1,487
Joined: 2002.09
Post: #3
Yeah, I'm not sure I'd go for the multithreaded route. The only thing it buys you is that your process is not blocked when swapping buffers due to vsync. That's only going to be an issue if your game would run at less than the vsync rate to begin with if unthrottled.

I found the best thing that you can do is to move away from using NSTimers to regulate your timing. My game loop is a tight while loop, update 0 or more ticks of logic until it catches up with the current time and draw a frame. The catch is that you have to pump the events yourself by calling
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource);
That simply pumps all the sources (events, timers, etc) and forces them all to update. Using an NSTimer, events are handled while the timer sleeps. You need to force the event actions to occur as you aren't allowing the flow of control to return to the main run loop.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Posts: 93
Joined: 2008.11
Post: #4
Thanks guys,

I have moved everything to single threaded model with the tricky code provided by Skorche and things seems to work a lot smoother now. Thanks!

Happy new year!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Logic Based Multiple View Game sum2cubes 1 3,740 Jul 2, 2009 08:30 AM
Last Post: bruss14
  question on init behavior kendric 3 3,483 Mar 27, 2009 07:55 AM
Last Post: kendric