Recursive Mutex In C++

Поделиться
HTML-код
  • Опубликовано: 6 июн 2024
  • JOIN ME
    -----
    RUclips 🎬 / @cppnuts
    Patreon 🚀 / cppnuts
    COMPLETE PLAYLIST
    ------------
    C++ Tutorial For Beginners: • Introduction To C++
    STL (Standard Template Library): • STL In C++
    ThreadIng In C++: • Multithreading In C++
    Data Structures: • Data Structure
    Algorithms: • Binary Search
    Design Patterns: • Factory Design Pattern...
    Smart Pointers: • Smart Pointer In C++
    C++14: • Digit Separator In C++
    C++17: • std string_view in C++...
    C++ All Type Casts: • static_cast In C++
    INTERVIEW PLAYLIST
    ------------
    C++ Interview Q&A: • Structural Padding & P...
    C++ Interview Q&A For Experienced: • How delete[] Knows How...
    Linked List Interview Questions: • Find Kth Node From Bac...
    BST Interview Questions: • Search Element In Bina...
    Array Interview Questions: • Reverse An Array
    String Interview Questions: • Check String Is Palind...
    Bit Manipulation Questions: • Find Set Bit In Intege...
    Binary Tree Interview Question: • Invert Binary Tree
    Sorting Algorithms: • Bubble Sort
    C++ MCQ: • Video
    C MCQ: • What printf returns af...
    C Interview Questions: • Designated Initializat...
    QUICK SHORT VIDEOS
    -------------
    C++ Short : • C++ Short Videos
    C Short : • Shorts C Programming MCQ
    In this video we will learn about Recursive Mutex In C++ (std::recursive_mutex). it is very helpful when we have to put lock in recursive function calls.
    Few points to remember about recursive mutex is as follows:
    0. It is same as mutex but, Same thread can lock one mutex multiple times using recursive_mutex.
    1. If thread T1 first call lock/try_lock on recursive mutex m1, then m1 is locked by T1, now
    as T1 is running in recursion T1 can call lock/try_lock any number of times there is no issue.
    2. But if T1 have acquired 10 times lock/try_lock on mutex m1 then thread T1 will have to unlock
    it 10 times otherwise no other thread will be able to lock mutex m1.
    It means recursive_mutex keeps count how many times it was locked so that many times it should be unlocked.
    3. How many time we can lock recursive_mutex is not defined but when that number reaches and if we were calling
    lock() it will return std::system_error OR if we were calling try_lock() then it will return false.
    BOTTOM LINE:
    0. It is similar to mutex but have extra facility that it can be locked multiple time by same thread.
    1. If we can avoid recursive_mutex then we should because it brings overhead to the system.
    2. It can be used in loops also.
    #threading #cpp #programming #tutorial #computerscience #softwareengineering

Комментарии • 36

  • @PhamAnhKhoa118
    @PhamAnhKhoa118 4 года назад +2

    Extremely helpful that opens my mind a lot. Tks!!!

    • @CppNuts
      @CppNuts  4 года назад

      Glad to hear it!

  • @krystianbednarczuk9466
    @krystianbednarczuk9466 4 года назад +5

    You doing a great job! Keep going :)

    • @CppNuts
      @CppNuts  4 года назад

      Sure man!!
      Thanks for the comment!!

  • @il4w
    @il4w 2 года назад

    Hi may I please know which software do you use to draw using that sketch style drawings ? You explain really well !

  • @cb559
    @cb559 3 года назад

    pratical impelementation in last would be cherry on cake!

  • @yadsarwat6325
    @yadsarwat6325 3 года назад +2

    I am beginner at C++, I came to it from java. Java learning is never a bad idea but I am enchanted to C++ by its speed and flexibility in coding world which ironically most of new programmer hates XD.
    But really great job at explaining the videos, really great explanations and simple. Keep the great job legend and I hope to see more of your videos related to C++.

    • @MitaliNeerPatel
      @MitaliNeerPatel Год назад

      you gonna love GoLang, it has more flexibility of writing codes and its much more efficient than java.

  • @subbumotepalli
    @subbumotepalli 4 года назад +1

    Great explanation.Thank u

    • @CppNuts
      @CppNuts  4 года назад

      Thanks for the comment dude!

  • @yogendragangwar9795
    @yogendragangwar9795 2 года назад

    Your thread seriese is awesome. :)

    • @CppNuts
      @CppNuts  2 года назад

      Thanks man..

  • @jotirlingswami9357
    @jotirlingswami9357 4 года назад +1

    Nice explanation Sir,
    we are waiting for your videos on c++

    • @CppNuts
      @CppNuts  4 года назад

      Thanks for the comment dude!!
      Sure there are so many videos coming on c++.

  • @Sha40ch
    @Sha40ch 3 года назад

    the video helped me a lot

    • @CppNuts
      @CppNuts  3 года назад

      Glad it helped!!

  • @ketanlalcheta4558
    @ketanlalcheta4558 3 года назад

    Hi I got concept of this...thank you very much....I have a doubt in example.. first coding example illustrates recursive lock... If recursive call is made from function , it could have been made post we do unlock.... i.e. do unlock first and then call recursive call.. does this not work...? General question is that assume recursive function has critical section but who or which limits us to unlock mutex before self calling?

  • @thestarinthesky_
    @thestarinthesky_ Год назад

    @1:53 and @4:44, it is actually not waiting for that lock , it will throw the run-time error if the same thread is trying to lock itself the second time.

  • @kevin-ru6oe
    @kevin-ru6oe 4 года назад +1

    if you can put the code in your blog, it would be very helpful for us. Thanks for your videos.

  • @thestarinthesky_
    @thestarinthesky_ Год назад

    @12:34 I'd place the code at line 38 inside the lock after line 36. Otherwise, it will cause some messy outputs on some random executions!

  • @pallavichaudhary6636
    @pallavichaudhary6636 Год назад

    Thank you Sir! I am giving interviews with the help of your videos only.
    One suggestion, just for visual better presentation. The comments section in your videos, the text colour is very light. Those points are very important, and while watching loose focus or interest on those points. Could you please change the colour of the comments section, to brighter colour? Thank you for all the valuable contents. Regards.

  • @kevin-ru6oe
    @kevin-ru6oe 4 года назад +3

    The code:
    // Example 1: With recursion
    #include
    #include
    #include
    using namespace std;
    std::recursive_mutex m1;
    int buffer = 0;
    void recursion(char c, int loopFor){
    if(loopFor < 0){
    return;
    }
    m1.lock();
    cout

  • @baskarvenkatesh7098
    @baskarvenkatesh7098 3 года назад

    excelent

  • @preetikhanna7034
    @preetikhanna7034 4 года назад

    There is something called recursive_timed_mutex.
    Can you give a video on it.

  • @sajit1988
    @sajit1988 4 года назад +1

    Please add videos for c++11 features, lamda function, for each etc..

    • @CppNuts
      @CppNuts  4 года назад +2

      Sure man now i am in mood to complete these 11/14/17/20 list.

  • @kunalprasad6898
    @kunalprasad6898 3 года назад +1

    Concept wise i understood but i have a question.
    Why to go for recursive mutex.
    It can be done by simple mutex .Just we need to take care that only one thread should be accessing the critical section and it can be done by keeping the function call in mutex.
    Means instead of keeping mutex inside the function why not keep mutex lock from where the fucntion is getting called?
    If I am missing some point where it can fail,Please let me know.

    • @sathwicksv
      @sathwicksv 2 года назад

      because the lock will not let the same thread to acquire lock again. results in deadlock

  • @anshugangwar4499
    @anshugangwar4499 4 года назад +1

    Nice work :) , please upload threading playlist code file on git hub

    • @CppNuts
      @CppNuts  4 года назад

      Thanks @Anshu..
      You can become a small member of my channel, then you can get code for any video. (It's very cheap)

  • @ergingokkaya8323
    @ergingokkaya8323 4 года назад +2

    I was afraid when the warning suddenly appeared😄

  • @GiorgiAptsiauriX
    @GiorgiAptsiauriX 3 года назад

    RUclips lacked you.