如何避免死锁问题 [英] how to avoid deadlock problem

查看:85
本文介绍了如何避免死锁问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当用户数量增加时,会发生死锁。如何避免它。

解决方案

请注意,这一点很少.net特定:< br $>


避免死锁的最大方法是锁定一致的顺序;这意味着你会得到常规的阻塞而不是死锁,但需要经过深思熟虑和规划你所锁定的内容以及何时锁定。当然,无论如何,这种想法和计划是必要的。



实现这一目标的一个简单方法是:尝试一次只需要一个锁定对象;因此,不是锁定A和B,而是锁定(单独)A然后B.这也需要思考和计划,但通常是可以实现的。



更一般地说,避免过度粒度锁定可以在这里节省大量的理智。对于可能竞争的锁定对象,要认真考虑使用两个概念的单个锁。在许多情况下,这不会对竞争所花费的时间产生太大影响,但会使代码更简单,更可靠。



实际上,我对语言有一种抱怨是采取锁定,但超时是比锁定更加代码密集。确保您始终有超时也可以确保完全锁定可以恢复。但这应该主要用于识别以错误顺序锁定的区域,以便您可以修复它们。





进一步详情请访问..



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 [ ^ ]







谢谢,

ketan


访问这里.......



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

In my application when the number of users increases ,deadlock occurs.How to avoid it.

解决方案

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


visit here.......

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


这篇关于如何避免死锁问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆