Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
In my application when the number of users increases ,deadlock occurs.How to avoid it.
Posted
Comments
lukeer 11-Sep-13 1:14am    
Write thread-safe code.
Sergey Alexandrovich Kryukov 11-Sep-13 1:15am    
Not enough information. And what you observe may even be something else, not a deadlock. The deadlock problem is inherently complex. In brief, thorough planning and logical analysis is the main thing.
—SA

Note that little of this is .net-specific:

The biggest way of avoiding a deadlock is to lock in a consistent order; this means you get regular blocking rather than a deadlock, but requires much thought and planning about what you are locking and when. Of course, this thought and planning is necessary anyway.

One simple way of achieving this is: try to only need one lock object at a time; so instead of locking A and B, you lock (separately) A then B. This too requires thought and planning, but is usually achievable.

Taking it more generally, avoiding over-granular locks can be a huge sanity-saver here. For lock objects that could compete, put serious consideration into just using a single lock of for both concepts. In many cases this doesn't hugely impact the time spent competing, but makes the code much simpler and more reliable.

Actually, one gripe I do have with the language is that "take a lock but with a timeout" is so much more code-intensive than "take a lock". Ensuring you always have timeouts can also ensure that a total lockup becomes recoverable. But this should mainly just be used to identify areas that are locking in the wrong order, so that you can fix them.


further details visit this..

http://msdn.microsoft.com/en-us/magazine/cc163618.aspx[^]

http://www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c15545/Deadlock-the-Problem-and-a-Solution.htm[^]



Thanks,
ketan
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900