promise And future In C++

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 73

  • @189Blake
    @189Blake 7 місяців тому

    In 8 minutes, you explained something it was taking me an hour to understand from books and documentation. Very well done!

  • @jamesdvc
    @jamesdvc 4 роки тому +9

    Best explanation I have read so far! Much appreciated and please keep the good work! Thank you!

  • @derekyingzhi8009
    @derekyingzhi8009 2 роки тому

    One of the best c++ educational videos I've watched. Very clear and informative. Thank you.

  • @hassangharbi3687
    @hassangharbi3687 Рік тому

    Very very well explained
    I'm french speaking and i had undestood almost everything

  • @TarunKumarSaraswat
    @TarunKumarSaraswat 4 роки тому +3

    You have explained this concept in most simplest way. Thank you :)

    • @CppNuts
      @CppNuts  4 роки тому

      You are welcome..

  • @ttmtv5128
    @ttmtv5128 4 роки тому +4

    I also admire your explanation level. Short and understandable.

    • @CppNuts
      @CppNuts  4 роки тому

      Glad it was helpful!

  • @kirannarayanaswamy2016
    @kirannarayanaswamy2016 2 роки тому +1

    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!

  • @kemijarks
    @kemijarks 2 роки тому

    you are very good at explaining , in an 8 minutes video.

  • @GiorgiAptsiauriX
    @GiorgiAptsiauriX 4 роки тому

    Excellent. These vids are giving me a good jumpstart into threading

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 3 роки тому

    Best explanation. Please keep uploading more videos on other topics. Great fan of yours sir!

    • @CppNuts
      @CppNuts  3 роки тому +1

      Thank you, I will

  • @igorbo8951
    @igorbo8951 2 роки тому

    Great explanations. I wish my professor could explain the same in 8 minutes.

    • @CppNuts
      @CppNuts  2 роки тому

      Glad it was helpful!

  • @paulb6611
    @paulb6611 2 роки тому

    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.

  • @VoidloniXaarii
    @VoidloniXaarii Рік тому

    Thank you, i think i understand a bit more thanks to you

  • @yea-yea
    @yea-yea Рік тому

    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.

  • @mdatifbinkarim3877
    @mdatifbinkarim3877 4 роки тому +3

    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??

    • @CppNuts
      @CppNuts  4 роки тому +2

      Yes you can pass as reference.

  • @RyantheCanuckpirate
    @RyantheCanuckpirate 2 роки тому +3

    shoutouts to Odd Future

  • @hoooga
    @hoooga 3 роки тому

    Great explanation!!

    • @CppNuts
      @CppNuts  3 роки тому

      Glad it was helpful!

  • @abimanyu86ece
    @abimanyu86ece 3 роки тому +1

    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?

  • @EA-zm7ge
    @EA-zm7ge Рік тому

    would it be also ok to pass the promise to the thread by reference? why to move? can you please explain?

    • @enjoynetsl
      @enjoynetsl 10 місяців тому

      My guess. There is an unique_lock inside promise, so it has to be move()

  • @fullface6104
    @fullface6104 2 роки тому

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

    • @ChefSalad
      @ChefSalad 3 місяці тому

      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.

  • @xiankunxu4432
    @xiankunxu4432 3 роки тому

    What's the difference when compare to using condition variables? Thanks.

  • @mdirshadalam2617
    @mdirshadalam2617 4 роки тому

    Very good explanation Rupesh,
    Thanks
    How to use template with promise and future?

  • @romk224
    @romk224 5 місяців тому

    Why you did "typedef long int ull" ? In the code it looks like "unsigned long long"

  • @treyquattro
    @treyquattro 4 роки тому

    personally I'm looking forward to the whatnot! Nice explanation.
    ETA: whatnot == C++20 coroutines?

  • @shilpaaggarwal5008
    @shilpaaggarwal5008 3 роки тому

    Short and crisp 👍

  • @kevin-ru6oe
    @kevin-ru6oe 4 роки тому +1

    In this case, it is not necessary the t1.join(), is it?

    • @CppNuts
      @CppNuts  4 роки тому

      No..

    • @sagisugyaan8975
      @sagisugyaan8975 4 роки тому

      we can do t1.detach() after creating thread in above case..however it is waiting at OddFuture.get() to get value.

  • @aerahtv0000
    @aerahtv0000 3 роки тому

    so it looks like it works similar to async/await in JS, with difference that async func runs in separate thread

  • @vijaynerkar4437
    @vijaynerkar4437 4 роки тому

    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 ?

  • @cid007007
    @cid007007 3 роки тому

    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);
    }
    ??

    • @pratikpandey3651
      @pratikpandey3651 8 місяців тому

      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.

  • @Jk_Vadaviya
    @Jk_Vadaviya 5 років тому +1

    Hi, Nice video, are you using a sublime text editor? how you get terminal there?

    • @CppNuts
      @CppNuts  5 років тому

      Its vs code.
      Not sublime text editor.

  • @abhishekdwivedi1506
    @abhishekdwivedi1506 4 роки тому

    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.

    • @CppNuts
      @CppNuts  4 роки тому

      If your future promise is not resolved then you will not go ahead, but if you are using global then you might mess up.

  • @ArsalanRaghib
    @ArsalanRaghib 5 років тому +1

    Is it the same concept as Rendezvous?

    • @CppNuts
      @CppNuts  5 років тому

      You can achieve with this.

  • @BHUSHANDHAWDE
    @BHUSHANDHAWDE 5 років тому

    Does order of joining thread and getting value from future matters?

    • @CppNuts
      @CppNuts  5 років тому +1

      It has nothing to do with the order of join.

  • @cid007007
    @cid007007 3 роки тому

    One more doubt can we set multiple values in thread "promise" and fetch them in "future" in main thread ???

    • @CppNuts
      @CppNuts  3 роки тому

      I guess change the data structure you are sharing then you can.

  • @aldrinaldrin4618
    @aldrinaldrin4618 3 роки тому

    2:26 I think I'm interested with your talent, care to sign in to my record label? xD

  • @tinuscariathomas7691
    @tinuscariathomas7691 4 роки тому

    please explain the usage of std::move

    • @CppNuts
      @CppNuts  4 роки тому +1

      That's great!!
      Noted, thanks..

    • @treyquattro
      @treyquattro 4 роки тому

      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)

  • @andreykrasnov7851
    @andreykrasnov7851 4 роки тому +3

    All these promises and futures are very confusing :(

    • @CppNuts
      @CppNuts  4 роки тому

      I would suggest, please watch it again.

  • @depdoprogramming2750
    @depdoprogramming2750 Рік тому

    great! thank you

  • @Hfrmobile
    @Hfrmobile 3 роки тому

    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!

  • @amargandhi1137
    @amargandhi1137 5 років тому +1

    Nice viedoes

    • @CppNuts
      @CppNuts  5 років тому +1

      Thanks man!!

  • @abhishekrai4677
    @abhishekrai4677 3 роки тому

    Greate One

  • @wadewang574
    @wadewang574 3 роки тому

    Is there the github source code, thank you ~

    • @CppNuts
      @CppNuts  3 роки тому

      Not yet!
      working on that.

  • @bestganesh
    @bestganesh 3 роки тому

    the voice is very low. otherwise nice video

  • @alexandrisaenkov175
    @alexandrisaenkov175 Рік тому

    🥰🥰🥰