What are the best practices/idioms should someone follow in order to 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.
Deadlock describes a condition in which two or more threads are blocked (hung) forever because they are waiting for each other.
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.
Please see What are common reasons for deadlocks?
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