Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid deadlocks in a multithreaded process

What are the best practices/idioms should someone follow in order to avoid deadlocks?

like image 543
Nizar Grira Avatar asked Jun 25 '09 13:06

Nizar Grira


People also ask

How do you avoid deadlocks?

Deadlock can be prevented by eliminating any of the four necessary conditions, which are mutual exclusion, hold and wait, no preemption, and circular wait. Mutual exclusion, hold and wait and no preemption cannot be violated practically. Circular wait can be feasibly eliminated by assigning a priority to each resource.

What is deadlock in multithreading?

Deadlock describes a condition in which two or more threads are blocked (hung) forever because they are waiting for each other.

How can we prevent deadlock synchronization?

Coming to your problem, it will not deadlock. If you have two independent attribute in a class shared by multiple threads, you must synchronized the access to each variable, but there is no problem if one thread is accessing one of the attribute and another thread accessing the other at the same time.


1 Answers

Please see What are common reasons for deadlocks?

like image 180
Mitch Wheat Avatar answered Oct 19 '22 23:10

Mitch Wheat