一种破坏TThread对象的正确方式 [英] A proper way of destroying a TThread object

查看:147
本文介绍了一种破坏TThread对象的正确方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能很简单,但我希望你不会忽略它。

在销毁一个TThread对象之前,通常需要等到调用TThread.Execute()方法的线程完成,因为只有这样,我们可以确定,例如,在类的析构函数内销毁的对象不再被访问。因此,有必要调用Terminate设置线程必须检查的Terminated标志,以确定是否退出,然后调用WaitFor()方法。

This question may seem trivial, but I hope you won't ignore it.
Before destroying a TThread object it is usually necessary to wait until the thread that called the TThread.Execute() method finishes, for only then can we be sure that, for instance, the objects destroyed inside the class's destructor are no longer accessed. Therefore it is necessary to call Terminate to set the Terminated flag that the thread has to check to know whether to exit or not, and then call the WaitFor() method.

因为线程可能被暂停,我认为在调用WaitFor之前恢复它是好的,否则调用线程将被锁定。而且因为线程可以暂停多次,应该恢复相同的次数,对吧?

Because the thread may be suspended, I think it is good to resume it before calling WaitFor, as otherwise the calling thread would be deadlocked. And because the thread can be suspended multiple times, it should be resumed the same number of times, right?

while Suspended do
  Resume;

如果线程被暂停,我们不用担心TThread.Execute()方法当我们恢复线程才能终止它 - 将不会(请纠正我,如果我错了)。

If the thread was created suspended, we do not have to worry that the TThread.Execute() method will be called when we resume the thread only to terminate it - it won't (please correct me if I'm wrong).

我所说的建议使用每个TThread对象被释放的以下代码行:

What I've stated suggests using the following lines of code for each TThread object being freed:

MyThread.Terminate;
while MyThread.Suspended do
  MyThread.Resume;
MyThread.WaitFor;
MyThread.Free;

不幸的是,当我们销毁已经创建了多个线程的应用程序时,为每个线程编写一段代码TThread对象被毁坏不必要地使得代码很长,甚至可能不透明。

Unfortunately, when we destroy our application that has created multiple threads, writing such a piece of code for each TThread object being destroyed unnecessarily makes the code very long and maybe even opaque.

因此,我得出结论,所有这些都可以放在TThread类的一个重写的析构函数中感谢它可以调用MyThread.Free(或MyThread.FreeOnTerminate设置为MyThread.Terminate),而不关心被破坏对象是否为TThread对象:

Therefore I came to a conclusion that all these could be put inside an overriden destructor of the TThread class thanks to which it would be enough to call MyThread.Free (or MyThread.Terminate if MyThread.FreeOnTerminate is set) without caring about whether the destroyed object is a TThread object or not:

destructor TMyThread.Destroy;
begin
  //if FreeOnTerminate, the calling thread cannot wait for itself
  if GetCurrentThreadId <> ThreadId then
  begin
    Terminate;
    while Suspended do
      Resume;
    WaitFor;
  end;

  {free all objects created in this class}

  inherited Destroy;
end;

原谅我问这样一个基本问题。但是,我想要通过这种方式了解您的意见 - 我希望通用的方法 - 销毁TThread对象。我问这个问题,我从我的同事的代码中学到,他们通常使用第一个代码来破坏这些对象,但是他们从来没有用来检查是否等待的线程被暂停,我认为线程有点危险可能会暂停在代码中的某个地方。因此,我试图找到一种普遍的方法来摧毁这个类的对象,这将使代码更加清晰和安全。我希望我没有变得更糟 - 你怎么看?

Forgive me asking such a basic question. I would like, however, to get to know your opinions about this way - I hope an universal way - of destroying TThread objects. I ask this questions for I learned from my workmates' codes that they usually used the first example of code to destroy such objects, but they never used to check whether the threads being waited for were not suspended which I considered a bit dangerous if the threads might be suspended somewhere in the code. Therefore I tried to find a universal way of destroying the objects of this class that would make the code clearer and safer. I hope I didn't make it worse - what do you think?

感谢您的建议。

推荐答案

您在TThread.Destroy析构函数中已经执行了很多建议,并且调用TMyThread.free将会做出你的建议。要清除线程类所拥有的任何对象,您可以在OnTerminate事件中执行此操作,该事件将作为线程关闭逻辑的一部分被调用。

Much of what your suggesting is already performed in the TThread.Destroy destructor, and invoking TMyThread.free will do just what your suggesting. To cleanup any objects owned by the thread class, you can perform that in the OnTerminate event, which will get invoked as part of the thread shutdown logic.

这篇关于一种破坏TThread对象的正确方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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