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

  Рет қаралды 15,214

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 32
@purpletom_yt3138
@purpletom_yt3138 4 күн бұрын
Hey amazing tutorial! Made it so simple! I can finally get a simple understanding on writing coroutines in C++! Keep up the good work! :)
@MikeShah
@MikeShah 4 күн бұрын
@@purpletom_yt3138 cheers!
@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
@lordmahakaal 11 ай бұрын
Async programming in c++ making sense to me now sir. Thanks a lot ❤
@MikeShah
@MikeShah 11 ай бұрын
Cheers!
@williamchidiac9421
@williamchidiac9421 2 жыл бұрын
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: kzfaq.info/get/bejne/odl6bMWVuL6ZiaM.html (Then make sure you link (with -L and -l) and include (with -I) the correct libraries to TTF.
@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.
@girinath2403
@girinath2403 2 жыл бұрын
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) :)
@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 8 ай бұрын
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.
@NorthstriderGaming
@NorthstriderGaming 6 ай бұрын
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 6 ай бұрын
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
@edmondw6689 2 жыл бұрын
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)
@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
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 88 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 36 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 70 МЛН
C# Async Await Mistakes | Part 1
10:19
Amichai Mantinband
Рет қаралды 32 М.
Using a trylock | Introduction to Concurrency in C++
18:30
Mike Shah
Рет қаралды 3,3 М.
How to make C++ run FASTER (with std::async)
23:10
The Cherno
Рет қаралды 259 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 837 М.
iPhone socket cleaning #Fixit
0:30
Tamar DB (mt)
Рет қаралды 18 МЛН
Better Than Smart Phones☠️🤯 | #trollface
0:11
Not Sanu Moments
Рет қаралды 17 МЛН
Это - iPhone 16!
16:29
Rozetked
Рет қаралды 138 М.
Какой ноутбук взять для учёбы? #msi #rtx4090 #laptop #юмор #игровой #apple #shorts
0:18
Как бесплатно замутить iphone 15 pro max
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН