谁使用 c# 中的 lock 关键字锁定了我的东西

发布于 2024-11-19 06:40:02 字数 64 浏览 1 评论 0原文

我的代码中有 lock 关键字。它冻结是因为其他东西锁住了这些东西。是否有某种命令或实用程序可以用来查看谁或什么?

I have the lock keyword in my code. It freezes because something else has the stuff locked. Is there some sort of command or utility I can use to see who or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-11-26 06:40:02

您可以使用 Windbg 附加到进程,并使用 sosex.dll 通过 dlk 命令查看哪些线程发生死锁。

http://www.stevestechspot.com/SOSEXV40NowAvailable.aspx

You can attach to the process with windbg and use sosex.dll to see what threads are deadlocked with the dlk command.

http://www.stevestechspot.com/SOSEXV40NowAvailable.aspx

z祗昰~ 2024-11-26 06:40:02

如果您使用锁,那么您应该在您控制的对象上使用它。通常,这是一个虚拟对象,仅用于您的锁,即

Object lockObj = new Object();
//some code
void MyCoolFunc()
{
    lock(lockObj)
    {
        //do some not threadsafe stuff
    }
}

If you are using lock, then you should be using it on an object you control. Usually, this is a dummy object only used for your locks i.e.

Object lockObj = new Object();
//some code
void MyCoolFunc()
{
    lock(lockObj)
    {
        //do some not threadsafe stuff
    }
}
嘿哥们儿 2024-11-26 06:40:02

Unfortunately, there is no simple answer ... but you can use tools such as CHESS to help you along the way. Ultimately, you will probably have to examine your code for race conditions that could result in deadlocks. Any place that you see a lock statement or other thread sync object (such as Monitors, Mutex, etc.) you will want to examine to see what they are locking on and find the other pieces of code that lock on the same thing

奈何桥上唱咆哮 2024-11-26 06:40:02

我认为您可以使用 VS.NET 附加到挂起的进程并单击暂停。然后调用“线程”窗口(“调试”-->“Windows”-->“线程”)并检查哪些线程被锁定以及原因。这看起来是最简单的解决方案。

I think that you can use VS.NET to attach to the hung process and click the pause. Then invoke the Threads window (Debug-->Windows-->Threads) and check which threads are locked and why. This looks to be the easiest solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文