How to make C++ run FASTER (with std::async)

preview_player
Показать описание


This video is sponsored by Hostinger
Рекомендации по теме
Комментарии
Автор

Someone possibly mentioned it already, but you can pass by reference to threads, you just need to use std::ref()

jovanrakocevic
Автор

Nobody:
The Cherno:
I need 19 Soviet trucks NOW

igorswies
Автор

if you preallocate a vector with number of "meshes", you can pass index to the async function, and you don't need to have locks.
Mutex and memory allocation are one of the top CPU eater, if you kill them performance will be increased a lot. Without preallocation of vector it will allocate more space few times during push_back. And with lock your algo will wait sometime until other thread will finish pushing back to vector. if you provide index to vector you can just run load_mesh without lock, and it willl be safe, because every thread will access unique index. Result should be much better.

oleksiimoisieienko
Автор

Parallel stacks view blew my mind. Didn't know such a feature exists!
Thanks.

malgailany
Автор

You could increase the performance even more by increasing the vector's size and passing just one reference to a cell as an argument to the function. That way you don't need to reallocate all the time + you get rid of the mutex.

TheOnlyJura
Автор

By far this is one of the best channel on youtube, thanks a lot for all this series.

gonzalosanchez
Автор

This How to finally showed me how to use async inside a loop. Did not find an answer on any of the 10 sites I've been on trying to find the answer.

You rock Cherno!

op
Автор

Dude, that's a great stuff, please keep it up!
Another suggestion would be multithreading + immutability, lock-free constructs.

vladuchoo
Автор

I just want to take a moment to contemplate the way he pronounced “cached”

guywithknife
Автор

Great video! You have always been such a nice teacher. One thing to add, possibly someone added it already: if we want to wait to get the returned value of all async(), we need to call get() of the future objects, for example, in your code ->

for (const auto& file : meshFilepaths) {
m_Futures.emplace_back(std::async(std::launch::async, LoadMesh, &m_Meshes, file));
}

/*
* if we wish to get the result value and keep processing
* we need to use get() of every future object
*/
for (auto& futureObject : m_Futures) {
futureObject.get();
}

Albert-lrky
Автор

Thank you for making this video I have wanted you to dive into multithreading a little deeper hope we can see some of this implemented in your game engine series rock on Cherno

ryanj.s
Автор

Amazingly precise presentation. Great job. Will be looking for your videos more.

SriNiVi
Автор

perfect, as always! Thank you, Cherno.

xxXSashaBardXxx
Автор

The reason the why the reference doesn't work is, that the std::async function takes the arguments for the function by copying them. When calling the function, std::async has has an own copy of the meshes, so LoadMesh function takes the variable of the std::async function. TLDR: the std::async passes the arguments by copying them (in lambda [=])

commanderguyonyt
Автор

Thanks for this video! You’ve helped a lot. I’ve never considered financially supporting anyone but you might be the 1st.

eddyecko
Автор

dude, you literally saved my life I was about to multithreading c++98 style before finding this by coincidence. *Thank you*
could make something about multiprocessor so far I didn't find anything to it in clean way in the standard library

abdosoliman
Автор

You missed out on one huge problem: std::async launches a new thread on *every* invocation. This burned me back in 2015 when msvc switched from the (better) threadpooling implementation to the standards compliant implementation. My program (altWinDirStat) can invoke upto millions of std::async tasks, so it hangs almost immediately at about 700 threads.

Ariccio
Автор

Mr. Chernikov, you are one of the best software/ game developers as well as teachers in existence right now. Thank you and kudos!

coolumar
Автор

Note that MSVC has a longstanding bug where `.valid()` on a future may erroneously return true even after calling `.get()`

chrisparker
Автор

I use async for loading a lot of textures. For saving some files I use a detached thread. I'm creating a map editor and I had some batch ops that I didn't want to wait or lock waiting on the writing to the hard drive. This video was very helpful.

Sebanisu
welcome to shbcf.ru