You are the best in the business! A simple and perfect example makes the basics very clear. Also, you explained that 3 + 5 = 8 secs concept of where the promise and future should be used. I enjoy your videos a lot along with new leanings every day. Helped me a lot thanks!
It might make sense for demonstration purposes to run something that is also computationally expensive without a std::future say findEven(). That way this would demonstrate two expensive operations running simultaneously.
Hi mate, thank you, great tutorial! However, I still have an open question: Why/When should some choose async over promise and vice-versa? Thanks again.
Awesome explanation. I have one query to know. As far as I have understood, the goal of using promise and future somehow close to getting some value from the thread function. Then if I just pass the OddSum as a reference to the thread function then would it be also OK??
Thanks for the nice explanation but I've a doubt, what happens if we do a t1.join before future.get()? Will it be still available or it is undefined behaviour?
In this code i don't understand the use of "t1.join()" call. I mean why put it near the end of code? Or the order in which you put this statement doesn't matter ? Just guessing...
The join call is just clean-up in this case. std::thread doesn't automatically join the two threads in its destructor like you might expect, which means that if you don't join then, when the function that created the thread finishes, the thread just hangs out in limbo as a memory leak. Obviously, this doesn't really matter in main, but it's good practice to always join threads anyway, even in main. Of course, really you should be using std::jthread which joins automatically, if you haven't already, in the destructor. This is also solves the problem of exceptions, since if something throws between creating the thread (hereafter: the new thread) and joining it, and you don't catch the exception in the thread that created this new thread, then the function will end without ending your new thread or joining it, which is a memory leak. This only matters if you catch the exception elsewhere, since your program will terminate if you don't catch it at all. std::jthread takes care of all of that, which is why you should use std::jthread instead of std::thread wherever possible.
Hi, Very good explanation. I am very easily understand multithreading. I have one question, If we put OddSumPromise.set_value(OddSum) into the for loop then program is crasing. Can you please tell me why ? Can you please explain what Semaphore ?
Only one doubt.. why we need to mention 2 times reference for promise parameter in function signature as shown below : void oddSumCall(promise &&oddSumPromise ,ull start, ull end){ ull oddSum = 0; for(ull i = start; i < end; i++){ if((i&1)==1){ oddSum += i; } } oddSumPromise.set_value(oddSum); } ??
the && means an rvalue reference it is used because when we use std::move() then the object(in this case the promise object) is typecasted to a rvalue. i.e it does not create a copy but passes as a reference.
The concept of getting value from user's function to main function is what future promise do but this can be achieved just by declaring global variable get it in main function then why use of future promise.
std::move() (& rvalue semantics generally) is probably the most confusing thing the C++ standards committee ever came up with, as far as most programmers are concerned. The good news is that there are a lot of videos on the subject. CppCon is the big annual C++ conference that helps to explain what's going on in the C++ world. All the presentations are on video here on youtube going back several years, just search for CppCon. Other very useful channels, in addition to CppNuts, are CppNow, CppOnSea, ACCU Conference, Meeting Cpp, and many others but those are the important ones in my book. In particular look for presentations by Nicolai Josuttis who is the big C++ cheese on move semantics (he's on the committee and has just written the definitive book on move semantics)
At the beginning it was difficult for me to understand the dialect... ;-) Btw, would name local variables camelCase always, not just sometimes ;-) Done for "start", "end" and "t1" but not for "OddSum" and "OddFuture".... I prefer would "oddSum" and "oddFuture" here. Good explanation anyway, thanks!
In 8 minutes, you explained something it was taking me an hour to understand from books and documentation. Very well done!
Best explanation I have read so far! Much appreciated and please keep the good work! Thank you!
Thanks dude..
One of the best c++ educational videos I've watched. Very clear and informative. Thank you.
Very very well explained
I'm french speaking and i had undestood almost everything
You have explained this concept in most simplest way. Thank you :)
You are welcome..
I also admire your explanation level. Short and understandable.
Glad it was helpful!
You are the best in the business! A simple and perfect example makes the basics very clear. Also, you explained that 3 + 5 = 8 secs concept of where the promise and future should be used. I enjoy your videos a lot along with new leanings every day. Helped me a lot thanks!
you are very good at explaining , in an 8 minutes video.
Excellent. These vids are giving me a good jumpstart into threading
Best explanation. Please keep uploading more videos on other topics. Great fan of yours sir!
Thank you, I will
Great explanations. I wish my professor could explain the same in 8 minutes.
Glad it was helpful!
It might make sense for demonstration purposes to run something that is also computationally expensive without a std::future say findEven(). That way this would demonstrate two expensive operations running simultaneously.
Thank you, i think i understand a bit more thanks to you
Hi mate, thank you, great tutorial! However, I still have an open question: Why/When should some choose async over promise and vice-versa? Thanks again.
Awesome explanation.
I have one query to know. As far as I have understood, the goal of using promise and future somehow close to getting some value from the thread function. Then if I just pass the OddSum as a reference to the thread function then would it be also OK??
Yes you can pass as reference.
shoutouts to Odd Future
Great explanation!!
Glad it was helpful!
Thanks for the nice explanation but I've a doubt, what happens if we do a t1.join before future.get()? Will it be still available or it is undefined behaviour?
would it be also ok to pass the promise to the thread by reference? why to move? can you please explain?
My guess. There is an unique_lock inside promise, so it has to be move()
In this code i don't understand the use of "t1.join()" call. I mean why put it near the end of code? Or the order in which you put this statement doesn't matter ? Just guessing...
The join call is just clean-up in this case. std::thread doesn't automatically join the two threads in its destructor like you might expect, which means that if you don't join then, when the function that created the thread finishes, the thread just hangs out in limbo as a memory leak. Obviously, this doesn't really matter in main, but it's good practice to always join threads anyway, even in main. Of course, really you should be using std::jthread which joins automatically, if you haven't already, in the destructor. This is also solves the problem of exceptions, since if something throws between creating the thread (hereafter: the new thread) and joining it, and you don't catch the exception in the thread that created this new thread, then the function will end without ending your new thread or joining it, which is a memory leak. This only matters if you catch the exception elsewhere, since your program will terminate if you don't catch it at all. std::jthread takes care of all of that, which is why you should use std::jthread instead of std::thread wherever possible.
What's the difference when compare to using condition variables? Thanks.
Very good explanation Rupesh,
Thanks
How to use template with promise and future?
Why you did "typedef long int ull" ? In the code it looks like "unsigned long long"
personally I'm looking forward to the whatnot! Nice explanation.
ETA: whatnot == C++20 coroutines?
Short and crisp 👍
Thanks..
In this case, it is not necessary the t1.join(), is it?
No..
we can do t1.detach() after creating thread in above case..however it is waiting at OddFuture.get() to get value.
so it looks like it works similar to async/await in JS, with difference that async func runs in separate thread
Hi,
Very good explanation. I am very easily understand multithreading.
I have one question,
If we put OddSumPromise.set_value(OddSum) into the for loop then program is crasing. Can you please tell me why ?
Can you please explain what Semaphore ?
Only one doubt.. why we need to mention 2 times reference for promise parameter in function signature as shown below :
void oddSumCall(promise &&oddSumPromise ,ull start, ull end){
ull oddSum = 0;
for(ull i = start; i < end; i++){
if((i&1)==1){
oddSum += i;
}
}
oddSumPromise.set_value(oddSum);
}
??
the && means an rvalue reference it is used because when we use std::move() then the object(in this case the promise object) is typecasted to a rvalue. i.e it does not create a copy but passes as a reference.
Hi, Nice video, are you using a sublime text editor? how you get terminal there?
Its vs code.
Not sublime text editor.
The concept of getting value from user's function to main function is what future promise do but this can be achieved just by declaring global variable get it in main function then why use of future promise.
If your future promise is not resolved then you will not go ahead, but if you are using global then you might mess up.
Is it the same concept as Rendezvous?
You can achieve with this.
Does order of joining thread and getting value from future matters?
It has nothing to do with the order of join.
One more doubt can we set multiple values in thread "promise" and fetch them in "future" in main thread ???
I guess change the data structure you are sharing then you can.
2:26 I think I'm interested with your talent, care to sign in to my record label? xD
please explain the usage of std::move
That's great!!
Noted, thanks..
std::move() (& rvalue semantics generally) is probably the most confusing thing the C++ standards committee ever came up with, as far as most programmers are concerned. The good news is that there are a lot of videos on the subject. CppCon is the big annual C++ conference that helps to explain what's going on in the C++ world. All the presentations are on video here on youtube going back several years, just search for CppCon. Other very useful channels, in addition to CppNuts, are CppNow, CppOnSea, ACCU Conference, Meeting Cpp, and many others but those are the important ones in my book. In particular look for presentations by Nicolai Josuttis who is the big C++ cheese on move semantics (he's on the committee and has just written the definitive book on move semantics)
All these promises and futures are very confusing :(
I would suggest, please watch it again.
great! thank you
You are welcome
At the beginning it was difficult for me to understand the dialect... ;-)
Btw, would name local variables camelCase always, not just sometimes ;-) Done for "start", "end" and "t1" but not for "OddSum" and "OddFuture".... I prefer would "oddSum" and "oddFuture" here.
Good explanation anyway, thanks!
Thanks
Nice viedoes
Thanks man!!
Greate One
Thanks
Is there the github source code, thank you ~
Not yet!
working on that.
the voice is very low. otherwise nice video
🥰🥰🥰
Thanks