How to use multithreading in C++

preview_player
Показать описание
In this video I show how to use std::async and std::thread to do multithreading in C++.

00:00 Intro
03:44 Std Async
08:44 Std Thread
12:49 Multithreaded Classes

Tools that I use:

In code:
Рекомендации по теме
Комментарии
Автор

This video is great, it's much better than most tutorials I could find online. My program runs 1.4x faster using multithreading!

tristanmisja
Автор

why do we have performance gain in multithreaded versions in the first place? i mean, task is moved to other thread, but we still have to wait for them to finish, thread.join() is blocking, isn't it? so, logically, it should be even slower because of thread creation overhead, i am confused

giorgikanashvili
Автор

I want to draw the ui in parallel in imgui, is it possible? Means do not wait for operation to complete, whenever info is available then draw, until then continue to draw frames

Sajal
Автор

Seems like you have a duplicate video starting around 16:13. Also, it would be cool to show a big feature of multithreading, which is rather than waiting for the calculation to complete every update loop, starting it in the background and polling whether it has finished or not. This would give an even bigger performance gain. Great video nonetheless!

romankovalful
Автор

You do not need to use packaged_task, you can just pass a lambda to std::thread, std::thread thread([]() { // code here }));

janwilmans