Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if two theads lock a mutex concurrently?

Tags:

People also ask

Can 2 threads lock the same mutex?

The short answer is "yes".

Can multiple threads take a lock simultaneously?

With locking, deadlock happens when threads acquire multiple locks at the same time, and two threads end up blocked while holding locks that they are each waiting for the other to release.

Can two threads share a mutex?

Only one thread can hold the mutex at a time, and the other thread can do no useful work.

What happens when mutex lock is used more than once?

Deadlock. If a thread that had already locked a mutex, tries to lock the mutex again, it will enter into the waiting list of that mutex, which results in a deadlock.


I'm interested in how a mutex works. I understand their purpose as every website I have found explains what they do but I haven't been able to understand what happens in this case:

There are two threads running concurrently and they try to lock the mutex at the same time.

This would not be a problem on a single core as this situation could never happen, but in a multi-core system I see this as a problem. I can't see any way to prevent concurrency problems like this but they obviously exist.

Thanks for any help