Multithreaded programming

Started by
23 comments, last by Khatharr 11 years, 1 month ago

I'm looking to make a little game project of mine and could use some advices.

I'll be using C++ and i'm aware of both SFML and SDL. Are there any other good 2D libraries out there for C++ that I should know of? Secondly, I wanna learn more about multithreaded programming so I want to make a game that would force me to use this.

Any advices on a game project and what 2D library to use would be appreciated.

Advertisement

As this is a For Beginner's post, I *STRONGLY* recommend you avoid multithreading for now.

Multithreading is an incredibly difficult thing to get right.

It makes existing bugs an order of magnitude harder to fix.

It creates new bugs that are almost impossible to trap in a debugger or logger.

After you are an experienced developer you will eventually be ready to handle multithreading. For now just realize that it is an advanced programming topic, mainly for those interested in masochism.

If you want to learn about multithreading, why not do a project just to test and learn the MT stuff, outside of a game? Trying to learn MT while at the same time doing everything you need for a game is really hard, and will mostly lead to frustration and pain when you're starting out. Some of the worst bugs to track down and fix are MT bugs. So, I'd recommend finding some project that you can do as a simple console application that is highly parallelizable, and learn the basics of MT from there.

Learn and practice one thing at a time. When you're competent enough with how MT works, then you can start to think how you can apply it to a game engine.

If you wan't to learn MT, go for simple app, like FTP Client-Server. Write it with C for the fun of it.

It'l teach you one thing i learned - Don't resort to MT.... like ever... unless really needed.

Most engines will hide MT from you, they let you execute your methods/functions every n frames or every n seconds.

If you are looking for a good 2D one that is C/C++, I really like orx, since it allows you to change things in your game really fast and supports nearly everything you need (such as physics and particles). The only drawback is that it has no network support, also, you won't be able to use threads on this one (but as I said, every engine I know will hide MT from you).

If you are really into learning MT, go for the classic problems, such as the dining phylosophers:

http://en.wikipedia.org/wiki/Dining_philosophers_problem

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

What kind of projects would allow me to focus on multithreading, while still building something?

If you want top-quality reading material with C++ (specifically C++11 and its standard threading library) and threading in mind, I can recommend C++ Concurrency in Action: Practical Multithreading by Anthony Williams (http://www.amazon.co.uk/dp/1933988770) and Herb Sutter's many articles (http://herbsutter.com/).

Pre-C++11 you can use boost::thread (nearly identical to std::thread in C++0x/C++11), so any articles concerning std::thread can also be applied to earlier versions of C++ supported by boost.

As people have said, multithreading needs to be threaded carefully (pun intended).

If you truly want to learn it, here's a fun thing you can do: make an audio player. With SDL/SFML or whatever GUI lib you decide to use, create a Load, Play, Pause, Stop buttons. Then there should be an audio playing in the background. Try playing a really big audio file (100MB WAVs), without loading it all up at once. Playing audio must be done on a separate thread. Load/Play/Pause/Stop buttons must be able to signal that thread appropriately, and the audio thread must signal back to the main thread.

alnite, on 25 Feb 2013 - 15:21, said:
As people have said, multithreading needs to be threaded carefully (pun intended).

If you truly want to learn it, here's a fun thing you can do: make an audio player. With SDL/SFML or whatever GUI lib you decide to use, create a Load, Play, Pause, Stop buttons. Then there should be an audio playing in the background. Try playing a really big audio file (100MB WAVs), without loading it all up at once. Playing audio must be done on a separate thread. Load/Play/Pause/Stop buttons must be able to signal that thread appropriately, and the audio thread must signal back to the main thread.

That's actually what I was going to suggest. +1

Before you get into that bundle of fun, though, you should probably just sit down and play with making small threaded programs that don't do much of anything, like looping counters and the like. Understand what mutexes and critical sections and such do and how to use them. Understand how to terminate a thread from within itself or from another thread. Get familiar with the functionality of your chosen thread-lib basically.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

What kind of projects would allow me to focus on multithreading, while still building something?

I wrote a multithreaded software raycaster one time and it was really fun, I then ported it to the GPU. I find some things work naturally for MT, the two situations I find it particularly useful in is either when you need to do the same thing over and over again on different data (eg. my raycaster) or when you need to do two tasks that have almost nothing to do with each other.

This topic is closed to new replies.

Advertisement