Ok, here’s what I’m trying to do:
Once Update() finishes its work, it tells a thread (or several threads) to run, calculating all the AI and physics for the next Update() loop while Draw() does its work. All of these threads will pause themselves once they have finished their work on that update loop.
I am not using a fixed timestep for the main program, but my physics updating is and Draw() will interpolate between states. I haven’t decided exactly what timestep I’ll use for the physics as I’m currently working on the prototype engine and want to get a feel for how the program will eventually do its work.
The idea behind this is I want the actual grunt work for the game is done in threads while Update() exists to prepare everything for the Draw() call and keep everything running smoothly and in time. The problem is I need to maintain time management to keep all the threads in sync.
I’ve played with Threads a little and I’m not going down the newbie route of just re-instantiating a new Thread object each Update(), as I’d prefer to preserve resources by looping within the same thread(s). Plus instantiating a new thread could cause the old one to be dropped before it has finished working if the system is running slow.
What searches I’ve done mostly finds things that are either not really appropriate for what I’m doing or they’re from an old discussions about using Thread.Suspend() and Thread.Resume(), both of which were depreciated after .Net 2.0.
The best I’ve found is a MSDN page detailing Thread.Sleep(Timeout.Infinite) inside the thread to pause it and then Thread.Interrupt() from another Thread (Update() in my case) to resume it. My problem with this method is it involves firing an exception to the target thread each time Interrupt() is called, and I don’t like the idea of firing exceptions that are not to do with errors.
I’m sure there are other people out there who have successfully used threads for updating elements of their game(s) so I’m curious about the methods they have used to manage this.
I’m also considering supporting super-high-refresh-rates (I use a 120hz monitor and I’ve seen one that can do 240hz) and I’m interested to know how much of a performance hit pausing and resuming threads like this would cause and if it could maintain such high update speeds.
Unless there is a better way?
Suggestions, ideas, advice and links are greatly appreciated.
Avan