std::future and std::async in Modern cpp | Introduction to Concurrency in C++

Ғылым және технология

►Full Series Playlist: • Modern C++ (cpp) Concu...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Git Repo: github.com/MikeShah/moderncpp...
►Lesson Description: In this lesson I teach you std::async and the idea that it returns a future. We block on the futures result if we need to compute the result, and otherwise std::async is an easy way to launch threads.
00:00 Introduction to std::async
1:11 What is a future
2:02 Creating a std::async function
3:27 Retrieving result with get
4:00 What is the point of async?
5:29 Waiting on a futures result
6:00 Progress is made in background on a task
►KZread Channel: / mikeshah
►Please like and subscribe to help the channel!

Пікірлер: 30

  • @arkadiuszuanowski5931
    @arkadiuszuanowski5931 Жыл бұрын

    As a second year student of CS who's having this the next week in his programming classes - thank you very much! You made it easily understandable

  • @MikeShah

    @MikeShah

    Жыл бұрын

    Cheers, thank you for the kind words!

  • @lordmahakaal
    @lordmahakaal9 ай бұрын

    Async programming in c++ making sense to me now sir. Thanks a lot ❤

  • @MikeShah

    @MikeShah

    9 ай бұрын

    Cheers!

  • @williamchidiac9421
    @williamchidiac94212 жыл бұрын

    Keep up the good work, Love your vids By the way I was watching your SDL playlist by i got stuck trying to install and set up SDL2_TTF mingw32 on windows do you have any advice on how i could set it up.

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    Thank you for the kind words! Sounds like a linking issue to me. Make sure you download the correct library for mingw here: www.libsdl.org/projects/SDL_ttf/ It may be a bit much, but hopefully this video can explain what is going on: kzread.info/dash/bejne/nad-m8Sdg7ubidI.html (Then make sure you link (with -L and -l) and include (with -I) the correct libraries to TTF.

  • @girinath2403
    @girinath24032 жыл бұрын

    Is std::async a better solution than creating a threadpool implentation for a client server model, where the server spins a new thread or call async for every client ? I was wondering about the implementation of std::async .

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    With a threadpool, you could initiate a specific number of dedicated worker threads at start up ready to perform some task (e.g. producer and consumer threads). You could spin up a thread 'asynchronously' to achieve this as part of your thread pool, rather than having a fixed-sized thread pool. std::async is just another means to create a thread, with less management than say std::thread.

  • @haddow777
    @haddow777 Жыл бұрын

    Kind of wondering about that example. It doesn't really show the benefit of async calls really because get appears to lock the system up. For instance, say I request some network data using an async call. I would like the data right away, but I have other tasks I can do if it's going to take a while for the data to come in. So, can I check the future in a non blocking fashion to see if it has gotten the data and move onto other tasks and just check in periodically. Or is it supposed to work like you make an async call to request the network data, then make async calls to carry out other tasks, then call get, which blocks this thread, but the other tasks will work independently while the main thread stays stuck on the get call until the network data comes in?

  • @MikeShah

    @MikeShah

    Жыл бұрын

    This is probably the simplest example to introduce the feature. The example with networking data you provide is a good one. Ideally you want to kick off some task to start retrieving data, then that can take place concurrently while you perform some other operation. The program should only be blocked when it's in a state where it must make use of data. The next video in the series shows one example of how to use the async call to do some task (e.g. loading video data) :)

  • @NorthstriderGaming
    @NorthstriderGaming5 ай бұрын

    So the way I understood this, std::future /std::async is primarily used to perform return operations on a different thread, while std::thread is being used for independed work. Did I get this right?

  • @MikeShah

    @MikeShah

    5 ай бұрын

    Sounds good to me! The future will otherwise 'block until' a result is given from the async operation (which may itself be running in a thread). std::thread otherwise starting a new control flow independently.

  • @edmondw6689
    @edmondw66892 жыл бұрын

    The actual execution of the async function happened at the time of the future is defined, or at the get?

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    The behavior in my example is actually undefined :) This means it will either wait until the result is used, or it can be spawned just like if we were to create std::thread. The next video I release I'll explicitly pass a parameter std::launch::async (in std::async) to have the thread start executing immedietely. We may otherwise want to use std::launch::deferred if we're okay with 'lazily' evaluateing our result at a later time, and blocking on that result (especially if it's something that can be computed quickly)

  • @WillJ-jy5tl
    @WillJ-jy5tl Жыл бұрын

    isn't this overall better than spooling threads with std::thread? We are reusing threads which reduce overhead cost

  • @MikeShah

    @MikeShah

    Жыл бұрын

    We could recycle our threads in a std::thread pool for instance to avoid spinning them up. I haven't done a comparison of async versus thread, though it is possible the implementation (though I'm not sure that's gaureenteed, would need to read the spec) of async has threads in a pool to reuse some computation and be faster.

  • @nighttime9539
    @nighttime9539 Жыл бұрын

    Hi Mike, thanks for your tutorial. However I still struggle to understand the advantage of 'async' instead of regular threads. Is it all about handling/retrieving 'futures'? Or is it about geting rid of threadpool in applications that run large amount of threads?

  • @MikeShah

    @MikeShah

    Жыл бұрын

    The use case or perhaps way to think of async is as a function call that executes in another thread, and only blocks the main thread if it hasn't finished computing the final result yet. The same could be achieved with regular threads, just the machinery is already setup for you.

  • @nighttime9539

    @nighttime9539

    Жыл бұрын

    @@MikeShah thanks, that makes sense

  • @MikeShah

    @MikeShah

    Жыл бұрын

    @@nighttime9539 Nothing wrong with allocating a pool of threads or build some sort of 'job system'. All of these are good patterns to think about. :)

  • @maxmustermann3938

    @maxmustermann3938

    7 ай бұрын

    The standard unfortunately makes no guarantee about how Async executes, i.e. it allows that it spawns a new thread but it also allows that it uses a thread pool. So it'll be different depending on the system you are running on. You'll have to carefully check which one is the case for the systems you are targeting, and may have to do your own thing depending on your use case.

  • @huyvole9724
    @huyvole9724 Жыл бұрын

    What are benifits?

  • @MikeShah

    @MikeShah

    Жыл бұрын

    Can make progress on one or many tasks concurrently.

  • @huyvole9724

    @huyvole9724

    Жыл бұрын

    @@MikeShah Sorry I didnt understand because we can you std::thread for concurrency, I meant why we use std::future promise, ....

  • @katakupro6523
    @katakupro6523 Жыл бұрын

    This is a bot

Келесі