Join And Detach In C++

Поділитися
Вставка
  • Опубліковано 27 жов 2024

КОМЕНТАРІ • 104

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

    Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
    And this will help me a-lot.

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

      Hey man, thanks a lot for this tutorial. Randomly picked your channel for understanding thread in code and it was worth it.

  • @ARIYANAMIRI-j9q
    @ARIYANAMIRI-j9q 8 місяців тому +1

    I was extremly tired of seeing every new born kid in youtube teaching C++ while i found your chanel,and I realy like your contents and i found them realy usefull just wanted to say , thank you man .

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

      Wow, thanks!

  • @debapriyoray8209
    @debapriyoray8209 2 роки тому +5

    Very nice explanation. Although I have a few doubts.
    1. What is the exact use of the detach function? I mean even if we create the thread and do not call the detach function is it not the same as creating the thread and calling the detach function?
    2. Coming to the destructor terminating the program if thread is joinable. The terminator will not be called until the end of the function here, so what is the issue in this case? Why do we need to use join or detach to make it non-joinable so that destructor does not terminate the program.
    Thanks in Advance

  • @mdshamshalam8925
    @mdshamshalam8925 5 років тому +3

    really really very good explanation. I have always confusion with Multi-threading in C++. Looking more videos on threading . Like for experienced persons . As C++ Developer company's always ask for Multi-threading in depth . Thanks alot .

  • @abelashenafi6291
    @abelashenafi6291 10 місяців тому +1

    So so Awesome man. Thanks for the easy-to-follow instructions and guides mate. Thank you

  • @_myron
    @_myron 5 років тому +39

    lol you confused me with the while (count --> 0). I thought it was some new syntax.

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

    Very good session talking about join vs detach. Will be a good idea to include how the main program can ensure that it is 'live' until the detached thread is running.

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

    Great video! Just I want to mention that when the process is destroyed (when the main function returns) no detached thread can live outside the main process. Furthermore, there is a phase when the process is being destroyed, the detached threads can exhibit undefined behavior if they ever touch static objects or variables of other threads

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

      Thanks.. it will be helpful to everyone..

  • @Ziemecki
    @Ziemecki 3 роки тому +7

    I think detach example was not really good. You forget to tell that when the main thread finishes, all threads will be stopped. Maybe it will be better to call a thread (t2) of a thread (t1) of the main thread. So when you detach t2 from t1, if t1 finishes, t2 will continue executing unless the main thread will finish.

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

    Amazing sir, thank you. That was so clear to me to understand this information. Easy. Have a nice day.

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

      Glad it was helpful!

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

    Sir you are a genius I'm glad that i found your channel. Superb explanation. Thank you so much 😊

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

      Thanks man!!
      Glad i could help..

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

    Hey Rupesh, I went through few of your videos and I really liked them as it helped me in brushing up my basics. You take us to the basics the and explain the things in detail. Thats the way it should be. Couple of suggestions or topics that I am looking forward:
    1. Joining the thread created in one class in another
    2. Different IPC mechanism(not sure if this is already there)
    3. It would be helpful if you can share the code that you are using to explain.(Actually, I need the header information from your code, which is short, sweet and meaningful)
    4. Difference between each version of cpp

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

      Thanks Parag, I will try but I am so much busy these days.

  • @amitpaunikar3408
    @amitpaunikar3408 5 років тому +2

    Nice video.... waiting for More videos on multithreading c++ 11

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

      Sure man!!

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

    You're kinda saying it wrong when mentioning that OS is being slow and doesn't execute the run(). The right way to say it is to explain that context switch(thread/process scheduling) depends on the OS scheduler and we cannot say what thread exactly will run at a particular moment. However, we can use locks for mutual exclusion and condition variables for ordering constraints, which were needed in the detach example.

  • @lukashk.1770
    @lukashk.1770 5 років тому +2

    really good explanation. looking forward for more vides on multithreading

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

      Sure, many videos are on the way.

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

    Nice... Easy to understand the concepts using these videos... Thanks for the same.... One doubt that came to mind is what's use case of detach ? As that task might not be completed at all (I thought it would be over after completion of other stuff in other thread or main thread...just detach means don't wait till completion
    .... Seems this understanding was wrong from my side.... So wandering what case detach usage is advisable)

  • @priteshugrankar6815
    @priteshugrankar6815 5 років тому +2

    You sir are a true genius.

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

      Thanks for the comment dude!!

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

    Please continue your good work. Very good explanation. Thanks a lot

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

      Thanks man!!

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

    Hi man, I love the energy from video. Very good explanation :)

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

    could you please suggest any real case example, when we need to detach a thread ?

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

    Good video once again. In case there was a detach for T1 thread, if main reaches the end, then the detached thread wont execute and program will terminate. Would be good to also have an example where T1 thread executes post initialization, control is passed to main thread and after a specified period, T1 kicks in once again.

  • @LetsDark
    @LetsDark 9 місяців тому +2

    `while (count --> 0)` is a crime

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

    Hi,
    I tried to run your code for add even/odd program using visual studio. But surprisingly using thread approach is taking more time than without thread approach.
    So, Can you share your comment on this?
    Regards,
    PK

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

    I am sorry if I missed this but a small confusion, i still couldn't undersand the functionality of detach, so it detachs the thread that we started and start running the parent thread, but if I am not wrong that behaviour would be same if we don't write anything at all ?

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

    Hey Rupesh , Its really good :) You are doing a good job ,, keep it up :)

  • @rajeshgalla12
    @rajeshgalla12 5 років тому +2

    Nice Video; When detach will be usefull while doing coding. This is because when parent process/thread is exited; then what is point of having other thread in coding.

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

      i think you got it wrong because either in join or detach case if main thread goes off then join or detach will also go off.
      We use detach just only for saying that that we are not waiting for this particular thread to complete.

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

      Its an important thought. I also wondered the same. Mainly detached thread is used for some background task to run and we don't care about it after that (its a completely independent thread and we are not interested in waiting for it to finish).

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 5 років тому +2

    Awsome Video..You are the Einstein of C++..

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

      kuch jyada nhi ho gaya?

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

      Parvez yee to inka pyar hai yaar, verna me to kuch bhi nahi..
      Mere liye C++ ka Einstein to Scott Meyers hai.
      He actually deserve this name, not me. 😊

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

      Chandra, Nahi yaar yee bahot badi baat keh di aap ne.

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

      @@CppNuts but Aapke video bahut acche hote hai.. aur easy to understand bhi 🙏

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

      Thanks man! 😊

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

    Why do you need to explicitly detach the thread using detach() that too in main.. while main ends by itself if join() is not used.

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

    thanks so much ...for making youtube video for other ...

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

      Welcome dude..

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

    What is the difference between using t1.detach() and not using join()?

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

    What's with /media/sf_SharedBetweenLinux/CppNuts Tutorial/CppNuts ? Are you running this all off a Pendrive?

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

    Well ! t1.detach() is mentioned to suspend the t1 thread execution then how it is still running inside main() thread ???

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

      Sorry if i understood wrong, but detach() doesn't suspend the thread, it just detach it from the process.

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

    Love your content!

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

      Glad you enjoy it!

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

    Hi Rupesh,
    Great thanks for your threading tutorial.
    There is a question comes in my mind:
    Shouldn't we consider refactoring when a function is too long to remember whether the thread has been already joined?
    Is there case that a thread is joined twice/multiple times?
    Best regards,

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

      Concurrently calling join() on the same thread object from multiple threads constitutes a data race that results in undefined behavior.

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

    Detach thread can release its resources , so what will be the impact i detached thread using other thread resources

  • @manjunathg8240
    @manjunathg8240 5 років тому +3

    Could you please demonstrate how a thread will terminate the program, if join or detach is not mentioned. Sample code in comment is fine.

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

      i think you didn't get it right because when program dies threads die but thread dont kill program.
      Re-watch the video for clarity.

    • @manjunathg8240
      @manjunathg8240 5 років тому +2

      @@CppNuts No sir. What I meant was like each main thread can have multiple sub threads. If any of the thread is crashed before main thread completion, then main program will terminate or only that particular thread will get killed.
      Case1 : when join is mentioned in main.
      Case2 : when join is not mentioned but added some 1min delay in main.

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

    what if run() returns some value lets say int. how will you return from the func executing by a thread.
    Can we detach this_thread in the run().?

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

      If you have to return a value from a thread you should look into std::async. async returns a std::future which holds the value returned from the callable object. If you can't use the async route, you could also return values by reference parameters. Make sure to pass your arguments with std::ref(), though. There is also std::promise but I haven't been able to wrap my head around that yet.

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

    how much i like you when you count points from 0. haha

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

    Thanks

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

    Sir how to create multi char server client using threads?

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

    Amazing video. You are a superstar

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

      You are the best

  • @beUnique-gq7iz
    @beUnique-gq7iz Рік тому

    🔥superb course

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

    Thanks

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

    is this threading concept used in transaction

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

    can you do videos on inter process communication in c++ please

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

    bro Could you please give link for functor?

  • @alexander1989x
    @alexander1989x Рік тому +1

    It's so cursed to see a "while" without curly brackets "{ }"

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

    4:18 just realized you sound like the terminator

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

    i saw x-->0 i thought that is it new syntex? and googling but it is x -- and >0 haha

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

    8:21 9:35 10:18

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

    I just cant stand how confidently you say "attends zero" when writing x --> 0 xDDD

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

    Missing Alexa

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

    The ADs are a little bit too much, which interrupts the thinking of learning

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

    you are doing a great job but why so much ads ?? :(

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

    after detach() execution main thread continues running
    after join() execution main thread pauses and waits until the new thread ends