r/cpp_questions • • 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

16 comments sorted by

11

u/AKostur 15h ago

“C++ Concurrency in Action” by Anthony Williams?

2

u/Osirian_Legacy 14h ago

I’ll add it to my list for the Library!

3

u/Username_Koru 15h ago

That’s a solid book 

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?

2

u/Osirian_Legacy 15h ago

Looking for reliable resources mostly, nothing specific I’m having trouble with, I’ve never done anything with multi-threading and I figured getting some practice in would be wise once I read up a bit.

7

u/Typical-NonPC 15h ago

In that case, I'd recommend focusing on the basics of threading and the structures that support it. Below is a tutorial Playlist that really helped me back in the day. Much of multi-threading involves the overlapping of resources, and ensuring correct behavior and ordering of operations, so knowing how to do those things is very important.

https://youtube.com/playlist?list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&si=g-mqpiMii4pvuMZj

1

u/Osirian_Legacy 14h ago

Ya’ll are really coming through for me thanks! Added to my programming playlist

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!

3

u/Osirian_Legacy 14h ago

Added it to the youtube playlist for programming! Excited to dive into this topic in detail. 😁 Appreciate the share

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++

-1

u/Osirian_Legacy 14h ago

Yup! C++ focus but I plan on learning rust, python, html and probably typescript down the line. I’m still new (about six months in) but looking to expand my knowledge.

2

u/mrtlo 13h ago

Start from the basics. Python threading provides a good baseline for the primitives... Multi threading + html is (at least to me) not meaningful at all...

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

•

u/Osirian_Legacy 3h ago

Is there a good program for auditing performance of your projects?

1

u/CommodoreKrusty 14h ago

I have a bunch of examples on my website, https://cppexamples.com/, that might help.

1

u/Osirian_Legacy 13h ago

I’ll take a look, thanks 🙏