Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to successfully cause deadlock [closed]

Tags:

c#

deadlock

I'm trying to cause deadlock in C# for simulation purposes. Just a quick program.

Could anyone kindly suggest some ideas for doing so?

like image 455
Dot NET Avatar asked Jan 18 '12 11:01

Dot NET


People also ask

How can deadlock problem be resolved?

Deadlock frequency can sometimes be reduced by ensuring that all applications access their common data in the same order - meaning, for example, that they access (and therefore lock) rows in Table A, followed by Table B, followed by Table C, and so on.

How can we overcome deadlock?

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.


1 Answers

Something like this should work:

Thread 1:

lock (A) {

lock (B) { }

}

Thread 2:

lock (B) {

lock (A) { }

}
like image 132
Dot NET Avatar answered Oct 20 '22 02:10

Dot NET