Pausing and Resuming Threads every Update()

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.


You probably want to use something like wait handles:


The general idea:
You create all of your worker threads once and they ‘run’ all the time.
You create a wait handle for each thread, the worker thread waits on this signal, the main thread signals this when there is work for the thread to do and waits on the thread to signal it back when the work is done.

When you have multiple threads doing work for you, you can use WaitAll to wait for them to all complete their work before continuing.

Hope that helps!

Thanks for those links, that’s given me a lot of options to consider, I’ll be back once I’ve played with some of them.

Hi Avan!

I had the same problem when creating our game and I wrote a library to utilize multiple threads (and CPU cores).
Then .NETs Parallels library came around (has been some time since then). That one is a bit faster than our ThreadPool, but only works within loops and therefore only takes uniform delegates.

If you’d like to use different delegates (multiple calls to different methods) and still have them run in various threads and that all just a tiny little bit slower than the MS-native Tasks-Parallels library, then take a look at it.
I hope you’re able to fetch it. Just put it up on GitHub for you. (no license, no hassle)

Here’s the link:

If you have questions, just ask.

You can get through to me using
And, of course, this library is open to everyone interested.