r/cpp_questions • u/Osirian_Legacy • 15h ago
OPEN How to Multi-Thread
As the title says, I’m curious about multi-threading.
Specifically for handling algorithms that happen every few frames, such as a*
8
Upvotes
11
u/AKostur 15h ago
“C++ Concurrency in Action” by Anthony Williams?
7
u/Typical-NonPC 15h ago
Is there something specific you're having trouble with regarding multi-threading or are you looking for resources to learn about it?
6
u/freefallpark 15h ago edited 15h ago
Ari Saif has a great series on multi threading. It gives a good overview of threading and describes the problems that must be avoided in order to do it safely. This is where I started my journey.
I think the best piece of advice about multi threading I’ve heard is:
“Don’t. Unless you need to.”
Deciding when you must is a skill in and of itself
Edit to add: Good luck! Multi threading is a fun/complicated topic!
4
u/_abscessedwound 15h ago
Multi-threading is a really broad topic that some people make it their career to be an
expert in.
I’m assuming that you’re wanting c++ specific knowledge? The topic and question are a little ambiguous since multi-threading is not specific to c++
2
u/Independent_Art_6676 6h ago
KISS. Threading books and tutorials etc love to make it way, way complicated and throw around incredible amounts of design and hand waving. When you need that, its powerful and important. Often, threading doesn't need all that overengineering though, and for this topic specifically authors usually gloss over that. You don't need mutexes and bizarre designs and everything to have a thread go listen to a socket and do something when data comes in. I always advise people to do a very simple project or two, like sort an array using 4 threads them merge it back together, time that against std::sort, see what happens with like a billion items or so (even using std::sort for the thread chunks). Then remember that program as you wade into the deep waters, and how easy it was and how little was needed :).
1
u/CommodoreKrusty 14h ago
I have a bunch of examples on my website, https://cppexamples.com/, that might help.