• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
K4nkur0u

When to use multi-threading and when to not

11 posts in this topic

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
0

Share this post


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

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.
0

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).
0

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 [url="http://www.gamedev.net/topic/627258-how-many-average-number-of-threads-does-a-game-needs-regardless-of-simplicity/"]here[/url]. Edited by Telios
0

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
1

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
0

Share this post


Link to post
Share on other sites
[quote name='nfries88' timestamp='1345859861' post='4973166']
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.
[/quote]

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.
0

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.
0

Share this post


Link to post
Share on other sites
Add threads only for performance reasons.
[list]
[*]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.
[/list]
This above is a very weak classification. The need of multi-threading really depends on your design and your costumers.

Regards
Ömercan
0

Share this post


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

[b]However[/b], 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) ([i]if a non-realtime background thread steals our CPU time, maybe update will take longer than 33ms to run![/i]).

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0