From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Recursive mutex

Recursive mutex

- Olivia and I have been using this pencil as a mutex. Only one person at a time can own or have a lock on it, and only that person can access our shared resource: this notepad. - If I attempt to lock the mutex while another thread has it, my thread will be blocked and I need to wait until he unlocks it so it becomes available. - And if I attempt to lock the mutex, it doesn't appear to be available, so my thread will just have to wait too. - It's behind your ear. You already locked it. - Oh. Well, my thread can't unlock the mutex while I'm blocked waiting on it, and I'll be waiting on the mutex forever because I'll never be able to unlock it. I'm stuck and so are you. If a thread tries to lock a mutex that it's already locked, it'll enter into a waiting list for that mutex which results in something called a deadlock because no other thread can unlock that mutex. - There may be times when a program needs to lock a mutex multiple times before unlocking it. In that case, you should use…

Contents