C++ Multithreading with Thread and Async

Channel:
Subscribers:
2,650
Published on ● Video Link: https://www.youtube.com/watch?v=FxfHC7_s3NI



Duration: 54:31
181 views
4


It's not too bad to get code to run on other CPUs in modern C++. You just do something like this:
thread t(foo); //Runs the function foo() on another CPU
Then:
t.join(); //Waits for foo() to finish
Or:
t.detach(); //Don't wait for foo() to finish, it can do its own thing

What CPU does it run on? It's up to the thread scheduler, it might be a different CPU, it might even be your own.

Multithreading is nice and easy when working with pure functions (functions with no side effect), but if you are going to be accessing shared resources, use a mutex / lock_guard to protect the shared_resource so only one thread can touch it at a time