Jump to content
  • Advertisement
Sign in to follow this  
Player_0

Threads in a RTS

This topic is 5459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I want to ask if anyone can give me an idea of how many threads are going on in an average RTS these days. I haven't really worked with threads yet so I dont want to over or under do it. Would a thread for each unit be too much if im going to have 80-100 units? Or would it be better to do a few threads for the AI...Maybe break it into phases (Resources, Base stuff, Movement, Attack etc). If you move a unit should you generate a new thread to move that unit while the user can go on to do other stuff. I just want a general idea of a good practice for pentium processors I guess. Its under Windows if that matters.

Share this post


Link to post
Share on other sites
Advertisement
Typically only 1 thread is used for everything. Occasionally games may choose to use a separate thread for I/O bound code such as network communications and resource loading. However, games are mostly CPU bound code (ie performing calculations rather than waiting for I/O) for which multithreading doesn't provide any benifits on single processor systems.

Share this post


Link to post
Share on other sites
But I hear a lot of games are multithreaded dont you need threads in a game for everything to happen at once. Like for the AI to do its thing with its base and troops while you do yours, and for the world to interact and stuff. Is this all one thread?

Share this post


Link to post
Share on other sites
Quote:
Original post by Player_0
But I hear a lot of games are multithreaded dont you need threads in a game for everything to happen at once. Like for the AI to do its thing with its base and troops while you do yours, and for the world to interact and stuff. Is this all one thread?


Most of the time everything is in one thread, even if you have 2 threads it doesn't mean they execute at one (more like thread A and then B, then A again, etc). Using threads doesn't even guarantee that they execute for the same time and you wont know exactly when they are executing (or the order).

The most general game loop (one thread looks like this)

update Systems (sound, input, network, etc)
update Game (AI, other logic)
render Game (calculate frustum, send down to queue, etc)

HTH

Share this post


Link to post
Share on other sites
Here is an example of when to use threads:
If you see your CPU consumption is low but your FPS are low too it can be a sign that your system is waiting on the graphics card to much, that's a good sign to use threads. Let the graphics card do its thing and until it's done, do some AI in a different thread.

Share this post


Link to post
Share on other sites
Keep in mind that on a uniprocessor machine (which most home computers obviously are), nothing happens in parallel, anyway. Threads are executed in a round-robin fashion (or some close variation thereof); it's just a question of how small the time quanta are. Now, if the instructions are executed sequentially on the CPU, anyway, then adding unnecessary threads is only going to add overhead through scheduling, task-switching, and so forth.

Until multiprocessor machines become available, threading will not increase speed. It is useful to increase UI responsiveness and to handle asynchronous tasks, particularly when some tasks are blocking (such as a network I/O thread that may block on a recv or similar), but the game logic is, presently, better handled in a single thread. (Wait a few years until dual core CPU's become more prevalent, however, and we shall see what happens. 80–100 threads sounds like a vast amount even then, though.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Player_0
But I hear a lot of games are multithreaded dont you need threads in a game for everything to happen at once. Like for the AI to do its thing with its base and troops while you do yours, and for the world to interact and stuff. Is this all one thread?

You hear wrong [smile]

Everything doesn't happen it once. Say you have 3 things that you want to *appear* to be happening in your game at once, A, B and C. You can fake it by constantly looping over them like this:

* do 1st iteration of A
* do 1st iteration of B
* do 1st iteration of C
* render

* do 2nd iteration of A
* do 2nd iteration of B
* do 2nd iteration of C
* render

...

* do Nth iteration of A
* do Nth iteration of B
* do Nth iteration of C
* render

Each set of iterations is the same as a single frame (for example a game will do 20 sets of iterations per second if it runs at 20 frames per second). If you do this fast enough, the user can't tell that everything is only happening a bit at a time and it all 'blurs' together.

Share this post


Link to post
Share on other sites
You should really only use threads if you need concurrency. Adding threads is guaranteed to make your program more complex, and will almost always make your program slower, not faster. You might consider having a separate thread for something like an AI, but even then, it's probabably still better to just use scheduling on a single thread. If you're planning on using threads, you should design for it from the beginning, rather than trying to ram them in later on.

Share this post


Link to post
Share on other sites
Actually, a lot of the games do use multiple threads. There is a bit of extra overhead involved with synchronization, but if things are structured properly, it can actually be quite beneficial to use threads. In a program with a multiple number of threads, the CPU can continue to process data while: waiting for the GPU, waiting for I/O from the HD controller, waiting for I/O from the network card etc...

As for 80-100 threads running, depending on how they are being used (i.e. how often they sleep etc...), I wouldn't say that it is all that unreasonable. For instance, I just brought up the task manager on the system I running right now, and there are 373 threads running. You'll run into a problem if you have too many threads such that the CPU is spending most of its time with a context switch...

Michael Brennan, Ph. D.
Senior Device Technology Engineer
Spansion / Advanced Micro Devices (AMD)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should only use threads if there's a really good reason to. And there aren't many.

Some good times:
Network I/O (and most of the time these threads are invisible to you)
Loading Media
...

Really those are the only 2 that come to mind right now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!