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



Go to http://www.hostinger.com/cherno and use code “cherno” to get up to 91% OFF yearly web hosting plans. Succeed faster!

Patreon ► https://patreon.com/thecherno
Instagram ► https://instagram.com/thecherno

Twitter ► https://twitter.com/thecherno
Discord ► https://thecherno.com/discord

Series Playlist ► https://thecherno.com/cpp

This video is sponsored by Hostinger

49 thoughts on “How to make C++ run FASTER (with std::async)

  1. Async approach of loading files is only benificial on an SSD right? Doing this on an HDD would make the magnetic head bounce around to perform seeks at different locations, slowing down the reading process.

  2. just played around with this example, for people who do not use Windows nor VisualStudio, to compile this example you will need to add pthread to your linked libs, in cmake for me it worked by simply adding this
    target_link_libraries(${PROJECT_NAME} pthread)

  3. Great videos, I've been enhancing my code regularly just by watching your videos. one thing, in this one, didn't make sense though. I don't think owning a"mutex" locks access to a resource (the meshes vector). it rather forbids any other code that needs the same mutex to be executed while it's locked.

  4. coming from c# I was very confused that lock_guad does not know what resource to actually lock but after few minutes it clicked with me that mutex is being locked here, not vector

  5. C++ threads by design only accept objects by value. Trying to pass things by reference will also pass them by value. So, either pointer or std::ref is the way to go 🙂

  6. Awesome tutorial.
    I think it may have been easier if you returned a Ref<mesh> from LoadMesh(). I imagine that Ref is a trivially copiable pointer. Then in LoadMeshes have a vector of std::future<Ref<mesh> > and then call get on them. I usually prefer this pattern because then I don't have to use a global mutex on the shared container you pass in.

  7. You will trigger the viewer/coder by loading the same truck model 19 times.
    The brain immediately says, just load 1 truck and clone the models.
    I know that is not your intention but it's just a bad example.

  8. Hi Cherno, I love your videos about C++. They are very clear, helpfull and nice 😉 The only question that I have is to turn down the volume of the music in the background, becasue it's hard for me to get focused with the music 🙁

  9. I have a problem. I'm learning the use of C++ threads, async, etc. and I want to run in parallel a function belonging to myClass.

    So, in myClass there are:
    1) int doSomething(int ini, int end);
    2) void manageThreads();

    I'm doing this:
    void myClass::manageThreads()
    {
    int ini=0;
    int end=100;

    std::future<int> ft1(std::async(this->doSomething, ini, end)); //ERROR HERE, WHY ?
    }

    The ERROR is: 'temp::fun': non-standard syntax; use '&' to create a pointer to member

    Can someone help me to resolve this error ?
    Thanks

  10. Well this just makes me appreciate the simplicity of C#’s concurrency model, not even looking at Go’s model which is somehow even simpler, all this could be done with a simple foreach, a Task list and and Task.AwaitAll

  11. isn't it better not to copy mesh into mashes by doing push_back, but rather do push_back(move(mash))? Or am I wrong and copy elision will rull this out and automatically call move version of push_back?

  12. OpenACC is also an option, as it allows use of GPUs without losing execution generality (if no GPU is detected it behaves like OpenMP). Or just go ballistic and use CUDA/OpenCL 🙂

  13. 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.

  14. Threading is awesome, but must be done judicially. You want each thread to do a "significant" amount of work, otherwise, overhead can kill you. The amount of cycles, wall clock time, and cache switching required to deal with mutexes, semaphores and condition variables can easily equal 1000+ mundane operations. Clearly, if you need a user interface to pop up before resources are all loaded, that can only reasonably be done in threads. But if the purpose is performance computing, you need to make the numbers.

  15. As a personal thing to confess my own embarrassment, I got lost pretty early on in this video 😅. I think my brain mainly got put off-kilter while trying to take in all the code that was already written for loading assets

Leave a Reply

Your email address will not be published. Required fields are marked *