Jump to content

  • Log In with Google      Sign In   
  • Create Account


When to use multi-threading and when to not


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 K4nkur0u   Members   -  Reputation: 158

Like
0Likes
Like

Posted 24 August 2012 - 01:45 PM

Just as the title says. Lets say I am programming a game from scratch in C++, when should I use multi-threading or when not? I don't have much experience on the subject. Thanks

Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2158

Like
0Likes
Like

Posted 24 August 2012 - 02:09 PM

Hi,

Using separate threads for certain game program components such as audio streaming and networking come quite naturally and may be added when the program requires it.

Otherwise separating the workload on multiple threads correctly isn't always that simple. It requires you to make design decisions in the early phase and since you aren't familiar with the subject, I don't see it to be a simple task. Adding multiple threads afterwards may turn out to be quite difficult too.

Answering your question isn't easy. Multiple threads can be used in many applications, but I wouldn't rush into before defining the problem more precisely.

In my personal engine project I'm using multiple threads in audio streaming and networking. I'd like to implement threads elsewhere too such as for physics, but when ever I check the CPU load (which stays at few %) I realize that I don't need it yet.

Of course in order to squeeze all the performance of the current and future CPUs, using multiple threads is necessary.

Cheers!

Edited by kauna, 24 August 2012 - 02:10 PM.


#3 orizvi   Members   -  Reputation: 266

Like
0Likes
Like

Posted 24 August 2012 - 02:15 PM

You should use multi-threading for tasks that don't have a lot of dependencies (preferably no dependencies) and where you're finding a single thread is insufficient. Its probably quite unlikely that a single thread is insufficient so I'd recommend: Don't use it at all.

#4 phil_t   Crossbones+   -  Reputation: 3203

Like
0Likes
Like

Posted 24 August 2012 - 02:18 PM

I would say never, unless you need to for one of these reasons:

- avoiding UI hangs for I/O operations like reading/writing to disk, or networking (even so, asynchronous APIs may hide the underlying multi-threaded nature of these. For instance the networking and audio functionality built in to XNA is multithreaded under the covers, but you access it all from a single thread).

- spread workload over multiple cores to help give an incremental improvement with performance. But it should be a last resort. You have the potential for much greater performance gains with algorithmic improvements than you do with multi-threading (which requires a lot of very careful consideration).

#5 Telios   Members   -  Reputation: 398

Like
0Likes
Like

Posted 24 August 2012 - 04:21 PM

There was a thread about this recently. The general wisdom seems to be that you should only use multiple threads if your game has been programmed sensibly, but still can't achieve interactive frame rates on a single thread.

Often the APIs you're using will use multiple threads internally, such as the FMOD sound system. There's no need to do it yourself.


If disk I/O is a problem, it's easier to use the asynchronous I/O provided by the operating system rather than multiple threads. If data processing is the bottleneck, it's probably better to process your data files offline and have the game read simpler binary structures from disk.

Hope that helps.

[Edit] The earlier thread (no pun intended) is here.

Edited by Telios, 24 August 2012 - 04:23 PM.


#6 mikiex   Members   -  Reputation: 237

Like
1Likes
Like

Posted 24 August 2012 - 04:43 PM

Its worth mentioning this advice is for PCs, on a console you will be multithreading but then you don't have the OS getting in the way

#7 K4nkur0u   Members   -  Reputation: 158

Like
0Likes
Like

Posted 24 August 2012 - 05:27 PM

Thanks guys, makes perfect sense.

#8 nfries88   Members   -  Reputation: 259

Like
0Likes
Like

Posted 24 August 2012 - 07:57 PM

The explicit purpose of multi-threading is to allow simultaneous execution of multiple tasks.

If the problem does not involve more than one task, using multiple threads should be avoided.

In the case of the game, you have three tasks (input, processing, and output), and Operating System limitations often require input and [visual] output to be done on the same thread. Processing (ai and physics) and audio output are deserving of their own threads.

Edited by nfries88, 24 August 2012 - 07:58 PM.

Looking for paid or open-source C++ programming work. Been programming since 2005. No degree.

#9 Trienco   Crossbones+   -  Reputation: 2051

Like
0Likes
Like

Posted 24 August 2012 - 10:45 PM

The explicit purpose of multi-threading is to allow simultaneous execution of multiple tasks.

If the problem does not involve more than one task, using multiple threads should be avoided.

In the case of the game, you have three tasks (input, processing, and output), and Operating System limitations often require input and [visual] output to be done on the same thread. Processing (ai and physics) and audio output are deserving of their own threads.


I would have to strongly disagree with your definition of tasks in a game. Trying to put physics, ai and rendering in separate threads is a mess of endless dependencies and I was under the impression this misguided approach has been abandoned by most game developers years ago.

Tasks are way smaller than that. Updating ONE enemy AI (or rather #enemies / #cores) is a task,if those updates are independent of each other. The whole beauty of a task based approach (which is NOT putting the entire ai, physics, etc. in single threads) is that with a bunch of worker threads, you can easily do multithreading without creating a huge mess of dependencies and mutex locking.

If you find that your ai updates take too long, it might be a trivial matter of replacing the for-loop with a parallel_for.

Also pipelines can be good places for multithreading (already nicely supported by TBB, a library I'd consider almost perfect for a task based approach). A process like loading a level might require

-loading the data from disk
-uncompressing the data
-process the data (into vertex buffers or whatever)
-upload geometry/textures to the video card

Each step can be done in parallel and some steps can even be done in multiple threads. TBB usually takes care of assigning threads to these steps (you just define the steps and which can be done in parallel), but one way might be:
-1 thread loads files and unzips the data
-2 threads do the heavy lifting of processing the data
-1 thread (set to always be the main thread) uploads finished data to the video card.

Depending on existing code, this could be added in half a day, if load times turn out to be a problem. It's what I did with my Minecraft clone and the neat things is that it didn't screw with any other part of the code.

Though in general I'd agree that unless you see the need for it, don't burden yourself with the non-deterministic, sometimes almost impossible to track down subtle bugs that can't even be easily reproduced. If Dante would have known about computers, one level of hell would be debugging multithreaded applications for all eternity.
f@dzhttp://festini.device-zero.de

#10 slayemin   Members   -  Reputation: 2050

Like
0Likes
Like

Posted 25 August 2012 - 05:52 AM

Just to add, there is a cost vs. benefit ratio for multithreading.

I once wrote a distributed program which had multiple networked computers doing a bunch of processing. My first thought was to create LOTS of threads and assign them small tasks. Intuitively, this would seem to be very fast since the work is very small and broken down among multiple machines. Result: I was wrong.
The overhead cost of creating tons of small tasks and sending them through a network was more expensive than the gains made from multiprocessing.

There is a sweet spot for each job. More threads doesn't necessarily equal faster performance since there's always an overhead cost. If you're going to pay the overhead cost, make sure you're getting the most bang for your buck :) In my case, it meant sending larger chunks of work to each computer. The size of work to send out had to be found through a bit of trial and error. Sending my job out to multiple computers was much faster than having one computer do the processing, but sending out the work in really small chunks was even slower than running on a single machine.

Eric Nevala

Currently a self-employed indie game dev


#11 omercan   Members   -  Reputation: 370

Like
0Likes
Like

Posted 25 August 2012 - 09:05 AM

Add threads only for performance reasons.
  • When you do a simple application, don't use them.
  • When you use heavy input and output operations or do other long tasks. Use predefined threads. Like two threads, first for the main loop and the second for asynchronous operations.
  • Or you want an scalable and fast application (mostly engines), then you should use task based threads (like TBB). But they are only useful for computers with more then two cores.
This above is a very weak classification. The need of multi-threading really depends on your design and your costumers.

Regards
Ömercan
Reading, Reading, Reading... why do you read not more?
I have a blog: omercan1993.wordpress.com look there for more content :)
And I also do some art: omercan1993.deviantart.com
And whats about the Pear3DEngine? Never heard of it? Go and look!
Yeah, and currently I do this: SimuWorld

PS: Please look at this poll on my blog about scripting languages: A opinion poll about scripting language

#12 Hodgman   Moderators   -  Reputation: 27686

Like
0Likes
Like

Posted 25 August 2012 - 09:56 AM

At an OS level, the point of threads is to allow the computer to do two things at once, e.g. keep a GUI responsive while processing a large file.

However, this is not the case with games. Games are realtime applications, which means the execution times of our functions always has to have an upper bound -- i.e. the Update function in our main loop is written in such a way that it only ever takes 33ms, etc... So, we don't need threads for this purpose, and using threads for this purpose is actually harmful, as it increase the unpredictability of our "main" thread(s) (if a non-realtime background thread steals our CPU time, maybe update will take longer than 33ms to run!).

So, the purpose of threads in realtime games is instead only to take advantage of multi-core CPUs. If you have some task that is so computationally expensive that it cannot complete fast enough, then you can optimise that task by splitting it over several CPU cores, which is an advanced optimization topic.

If your game runs at 30Hz on a single CPU core, then you do not need to bother with multi-threading.

Edited by Hodgman, 25 August 2012 - 10:03 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS