Threads in a RTS

Started by
18 comments, last by DigitalDelusion 19 years, 6 months ago
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.
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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?
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

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.
Don't shoot! I'm with the science team.....
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.)
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
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)
Michael Brennan, Ph.D.
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.

This topic is closed to new replies.

Advertisement