Definition
Task/Thread/Process is waiting to acquire the semaphore which will never become free.
Example
What are the causes of deadlock?
How to prevent deadlock?
- Avoid having locksAlways take the locks in the same order.
The common solution to preventing deadlock in code is to make sure the sequence of locking occur in a common manner regardless of which thread is accessing the resources.
For example given threads T1 and T2, where T1 accesses resource A and then B and T2 accesses resource B and then A. Locking the resources in the order they are needed causes a dead-lock. The simple solution is to lock A and then lock B, regardless of the order specific thread will use the resources.
Problematic situation:
Thread1 Thread2
------- -------
Lock Resource A Lock Resource B
Do Resource A thing... Do Resource B thing...
Lock Resource B Lock Resource A
Do Resource B thing... Do Resource A thing...
Possible Solution:
Thread1 Thread2
------- -------
Lock Resource A Lock Resource A
Lock Resource B Lock Resource B
Do Resource A thing... Do Resource B thing...
Do Resource B thing... Do Resource A thing...
lock_guard
and unique_lock
are pretty much the same thing; lock_guard
is a restricted version with a limited interface.
A lock_guard
always holds a lock from its construction to its destruction. A unique_lock
can be created without immediately locking, can unlock at any point in its existence, and can transfer ownership of the lock from one instance to another.
So you always use lock_guard
, unless you need the capabilities of unique_lock
. A condition_variable
needs a unique_lock
.
How to detect?
jcmd $PID Thread.print
The above command can be executed only for java programming.
Comments
Post a Comment