Sign in to follow this  
ibiza420

How many threads for a game?

Recommended Posts

Hi all, I am fairly new at developing games (for fun), but not at programming. I am attempting to make a simple demo in C# for a game I may (or may never) code in DirectX one day. For now, I am only using GDI+ to toy around with functionalities such as timing, objects managment, etc. My question is : how many threads a game really needs? I mean by that, that if a game has a single thread, used to generate game objects and to animate the scene as well, what will happen if there is a big lag? My guess is that not only the graphics will lag, but also the object creation engine, which may skip a few objects (enemies, gun shots, etc.) to create (probably resulting in an easier gameplay as not all objects that should be there aren't?). So is there a need to have 2 threads? One for object creation, which should be as fast and accurate as possible, and another one for animation, whose running speed depends on the framerate wanted? Thanks for helping me! ibiza

Share this post


Link to post
Share on other sites
You usually won't need threads for creating or animating objects. These should be able to be performed on the fly during the game. You will typically use threads for networking and audio. Or for a menu system that does a lot of processing but needs to be responsive. I'm about to write a texture manager class that will have to load textures in threads because it takes ~10 seconds to load all the textures I need.

But to answer your question: games will use any # of threads. If used correctly, each thread will have very little overhead because the cpu will only call the thread when it is needed.
I.E: don't have a thread that has a spin lock. For those of you that don't know,

while (!thread_ready); //wait until thread is signal'd by another process.

This is bad because the thread will process that loop a million times each frame. Instead use WaitForSingleObject & WaitForMultipleObjects to wait on threads or mutexes. Also, threads work well with networking calls that block and wait for msgs.

Share this post


Link to post
Share on other sites
Hi, thanks for your reply!

I was asking the question because in my case, I have a simple program where I can adjust the refresh rate of my main loop (which handles both the object creation AND the animation), with the help of a slider. I have a "Shot generator" whichs generates simple circles for now, each one with the same constant speed and direction The interval at which these circles are generated is also constant : one every 100ms (or 10 / second). What happens when I decrease the frame rate to less than 10fps (let's say 5), is that there are no more 10 circles per second generated, but rather 5. And it's normal, sinces the loop is only executed every 200ms when the framerate is at 5.

So what would be the solution in my case to still generate 10 circles per second, but update their position only at every 200ms?

This is the problem I am actually facing...

thanks! :)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this