This is an interview question . In general the deadlock between 2 threads is generated when thread1 locks mutex1,and a moment before it tries to lock mutex2 ,thread 2 locks mutex2.After that tread 2 wants to lock mutex1.So they wait for each other forever.
The question was "Can you gave a scenario of deadlock with one mutex and any number of threads?"
A deadlock requires 4 things:
Mutual Exclusion - refers to the idea of having a resource (a lock) that can be owned by a single thread.
No pre-emption - locks cannot be acquired forcefully
Circular Wait - refers to one thread waiting on another, that is waiting on itself (or a chain)
Hold and Wait - a thread is able to take one lock and wait for another
In an interview, it's usually more important to show them your thought processes and if you bring up these rules, they'll probably do more for you than trying to give a 'technically correct' trick answer.
You could do it this way, though:
Master thread locks a resource, then sends some tasks to the thread pool. These tasks wait for the resource. The master thread waits for the tasks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With