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
Extremely helpful that opens my mind a lot. Tks!!!
Glad to hear it!
You doing a great job! Keep going :)
Sure man!!
Thanks for the comment!!
Hi may I please know which software do you use to draw using that sketch style drawings ? You explain really well !
pratical impelementation in last would be cherry on cake!
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++.
you gonna love GoLang, it has more flexibility of writing codes and its much more efficient than java.
Great explanation.Thank u
Thanks for the comment dude!
Your thread seriese is awesome. :)
Thanks man..
Nice explanation Sir,
we are waiting for your videos on c++
Thanks for the comment dude!!
Sure there are so many videos coming on c++.
the video helped me a lot
Glad it helped!!
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?
@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.
if you can put the code in your blog, it would be very helpful for us. Thanks for your videos.
@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!
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.
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
excelent
Thanks..
There is something called recursive_timed_mutex.
Can you give a video on it.
Please add videos for c++11 features, lamda function, for each etc..
Sure man now i am in mood to complete these 11/14/17/20 list.
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.
because the lock will not let the same thread to acquire lock again. results in deadlock
Nice work :) , please upload threading playlist code file on git hub
Thanks @Anshu..
You can become a small member of my channel, then you can get code for any video. (It's very cheap)
I was afraid when the warning suddenly appeared😄
:D :D
RUclips lacked you.