Multithreaded programming

Started by
23 comments, last by Khatharr 11 years, 2 months ago
I think a good elementary use for multi threading is when you have a GUI that launches a background task. That's a fairly widely applicable kind of multi-threading. It comes up less often in simple games though, because they tend to need every calculation in every frame, so they just alternate between computing tasks, and drawing to the screen in a single thread.

In general, multi-threading is a way to achieve speedup, so it's not strictly necessary.

The best way to learn multi-threading is probably a book or course. There are a number of techniques that are used and which one is the best varies depending on your application, so you need to know all the tools in order to figure out how to parallelize a particular program. Unfortunately, in my experience courses have not been very comprehensive either, and I cannot recommend a good book.
Advertisement

The book The Art of Multiprocessor Programming, is a pretty good beginners book for this subject. However, it uses mostly java programming. But it can easily be transferred to the language of your choice.

does OpenGL handle multi-threading in the GPU for you when drawing?

also is there any big difference in performance if you use multi threading for games, say for your update functions ?

I haven't dealt with SDL that much, but I can tell you SFML actually has multi-threading, made relatively easy - there's a brief overview of the 1.6 version here: http://www.sfml-dev.org/tutorials/1.6/system-threads.php

SFML2 has some changes to the threading, making it (in my view) even easier, but the above tutorial is still useful.

Yes, as people have mentioned, multi-threading is a pain to debug and can take some time to wrap your head around it. But there's no clear point at which a programmer can be considered 'advanced' enough to start dealing with multithreading. In the end its just another tool, so as long as you're not discouraged by failure (we all fail when trying to do something, the point is to not give up), and don't mind banging your head against a wall (figuratively) for a while, then you could learn multithreading even as a beginner.

does OpenGL handle multi-threading in the GPU for you when drawing?


The GPU is it's own hardware that runs independently of the main CPU, so yes, kindof. It's more then just "a different thread" though, since it's entirely different hardware, and your shaders will most likely be run as not only one, but a bunch of threads in parallell.

also is there any big difference in performance if you use multi threading for games, say for your update functions ?

The gain you get with multithreading depends on the problem you try to solve.
If the tasks you do in parallell has no dependencies on each other, you might get close to NumberOfThreads times the performance, but a game engine states is by its nature quite dependent on each other, having lots of requirements on what needs to be done before what, and is very tricky to multithread in an efficient way.

Short Answer: its impossible to tell in general.

What multithreading library would you recommend for C++? POSIX, boost?

POSIX looks good

I recommend boost::thread if you are comfortable with C++ STL.

If I tried to explain threading in one paragraph, I would say that your worker thread function is absolutely safe if it does not interact with outside world: does not read/write any variable except it's local ones. Of course, this would be useless. But once you have isolated your worker thread, you can begin serving it the tasks to do and receive finished work. This serving/receiving process is the main trouble of threading, but once you identify it, it is a matter of synchronization. The most basic synchronization mechanism is allowing only one thread read/write the serve/receive queue, and we call this mechanism locking, which at lower level is entering and exiting a critical section.

I think it is crucial for programmers to get familiar with threading, learn it and use it when appropriate, because modern applications and our CPU architecture really demands it nowadays. However, you better know exactly what you are doing, because threads are very confusing to debug for obvious reasons.

I did a quick glance over several recommended guides and this one seems to be quite good: http://www.paulbridger.com/race_conditions/.

What multithreading library would you recommend for C++? POSIX, boost?

I would say the most portable and general purpose way to go is C++11 threads + boost. C++11 is incomplete in providing high level threading tools. They put all the basics in, but not all of the high level structures built on top of them.

Unfortunately, beginner material using the two is probably gonna be hard to find. C++11 is too new.

If the tasks you do in parallell has no dependencies on each other, you might get close to NumberOfThreads times the performance, but a game engine states is by its nature quite dependent on each other, having lots of requirements on what needs to be done before what, and is very tricky to multithread in an efficient way.

Slight nitpick: The speedup potential of parallel threads approaches the number of cores, not the number of threads.

This topic is closed to new replies.

Advertisement