简体   繁体   English

线程锁定在Delphi XE5应用中

[英]Thread locked in Delphi XE5 app

I have some random locks of my app apparently without a reason. 我的应用程序有一些随机锁,显然没有原因。

As I found on the embarcadero website I found some documentation that maybe was some thread waiting to be ended. 正如我在embarcadero网站上发现的那样,我找到了一些文档,也许是一些等待结束的话题。

When this happened I paused program execution from the IDE and went to the thread list. 发生这种情况时,我从IDE暂停了程序执行,然后转到线程列表。

This is what I got: 这就是我得到的: 线程列表

I'm quite stuck on this. 我对此很困惑。 How can I move further? 我该如何进一步?

How can I move further? 我该如何进一步?

If your application is built with debug info, then you can double-click any thread and you'll be taken to the line of source with the problem. 如果您的应用程序是使用调试信息构建的,则可以双击任何线程,然后将您带到有问题的源代码行。 You'll also get a call stack telling you the chain of calls leading to the 'current line' in each thread. 您还将获得一个调用堆栈,告诉您导致每个线程中“当前行”的调用链。

Of course in your case, the Wait Chain provides excellent clues where to start investigating the problem. 当然,在您的情况下,“等待链”提供了从哪里开始调查问题的绝妙线索。 This is where things get tricky, and only you will be able to debug further, because only you have all the source code. 这是事情变得棘手的地方,只有您才能进一步调试,因为只有您拥有所有源代码。

But as an overview: 但作为概述:

  • A blocked thread's call-stack will lead to some form of wait function. 被阻塞的线程的调用堆栈将导致某种形式的等待功能。 Eg WaitForSingleObject or TEvent.Wait or TThread.WaitFor . 例如, WaitForSingleObjectTEvent.WaitTThread.WaitFor
    • That thread won't continue executing until it's unblocked. 该线程将不会被执行直到被解除阻塞。
    • And you'd expect it to be unlocked from another thread. 而且您希望它可以从另一个线程中解锁。
  • You need to look up the call-stack to find which high level object it's waiting for. 您需要查找调用堆栈以查找它正在等待哪个高级对象。 Note it can be important to identify a specific instance and this is where it gets tricky. 请注意,识别特定实例可能很重要,这很棘手。
  • Once you've figured out exactly what is locked, you need to figure out why it hasn't been unlocked. 一旦弄清楚了什么被锁定,就需要弄清楚为什么它没有被解锁。 (Note your screen shot makes reference to owner threads and processes which is very valuable information.) (请注意,您的屏幕截图引用了所有者线程和进程,这是非常有价值的信息。)
    • You can double-click on other threads to examine where they are in their own calls stack. 您可以双击其他线程以检查它们在自己的调用堆栈中的位置。 (Note the threads are paused by the debugger, but not blocked; so you can find yourself anywhere within the call-stack.) (请注意,线程已由调试器暂停,但未被阻止;因此您可以在调用堆栈中的任何位置找到自己。)
    • You can also search other code that that interacts with the locked resource. 您还可以搜索与锁定资源交互的其他代码。 (In fact, sometimes this is necessary particularly in cases of problem #3 below.) (实际上,有时这是必要的,尤其是在下面的问题3的情况下。)
  • You'll have to examine the code to figure out why whatever resource is locked (and blocking other threads) hasn't been unlocked. 您将必须检查代码以找出为什么未锁定任何资源(并锁定其他线程)的原因。

Likely problems include: 可能的问题包括:

  1. Owning thread is simply slow and will unlock the resource eventually. 拥有线程的速度很慢,并且最终将解锁资源。
  2. Owning thread has a bug, and has entered an infinite loop preventing it from returning to unlock the resource. 拥有线程有一个错误,并且进入了一个无限循环,阻止其返回以解锁资源。
  3. Owning thread has a bug, and passed the code that should have unlocked the resource, but didn't. 所属线程有一个错误,并传递了应该已经解锁资源的代码,但没有。 (A common cause of this is poor exception handling; use your try..finally 's correctly.) (常见的原因是不良的异常处理;请正确使用try..finally 。)
  4. Owning thread is itself blocked. 拥有线程本身被阻止。 If it's blocked by another blocked thread, you've encountered a particularly insidious kind of block referred to as a deadlock . 如果它被另一个阻塞的线程阻塞 ,则您遇到了一种特别隐蔽的阻塞,称为死锁

In all the above cases, you need to understand your bug so that you can fix it. 在上述所有情况下,您都需要了解您的错误,才能对其进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM