Jump to content
  • Advertisement
Sign in to follow this  
K4nkur0u

When to use multi-threading and when to not

This topic is 2306 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

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

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!