简体   繁体   English

wxWidgets的wxThread删除了可连接的线程

[英]wxWidgets wxThread Delete for a Joinable Thread

I got a question about joinable threads in wxWidgets. 我有一个关于wxWidgets的可接合的线程问题。

When the user wants it, I want to stop a thread doing some work. 当用户需要时,我想停止执行某项工作的线程。 For that reason I call in this worker thread TestDestroy() to check whether the thread should be stopped. 因此,我在此工作线程TestDestroy()中调用以检查是否应停止线程。 But I can only stop the thread this way by calling Delete(), which should not be called for joinable threads. 但是我只能通过调用Delete()来停止线程,对于可连接线程,不应调用Delete()。

Is there a possibility for me to stop the thread (using TestDestroy) or do I have to change my code completely? 我有可能停止线程(使用TestDestroy)还是必须完全更改代码?

Thanks in advance, 提前致谢,

TiBo 钛宝

You have to call the Exit() method from your worker thread or simply return from the Run method AND call the MyThread->Wait() method then delete the thread object. 您必须从工作线程中调用Exit()方法,或者仅从Run方法中返回并调用MyThread-> Wait()方法,然后删除线程对象。

Declaring the thread : 声明线程:

class MyThread : public wxThread {
  virtual void * run();
};

Thread implementation : 线程实现:

MyThread::run()
{
  while(1)
  {
    if(TestDestroy())
    {
        this.Exit();  // or return;
    }
    // Do some work
  }
}

Declaring the Thread pointer : 声明线程指针:

MyThread * pMyThread;

Creating, launching and stopping the thread 创建,启动和停止线程

void launchThread{
  pMyThread = new wxThread(wxTHREAD_JOINABLE);
  pMyThread->Create();
  pMyThread->Run();
}

void stopThread(){
  pMyThread->Delete();
  pMyThread->Wait();
  delete pMyThread;
}

Hope that it helps. 希望对您有所帮助。

PS : this is my first answer on Stack Overflow. PS:这是我对Stack Overflow的第一个答案。 I don't know how I can easilly write some code automatically indented? 我不知道如何轻松编写一些自动缩进的代码?

You shouldn't have to rewrite your code. 您不必重写代码。

It's usually best that a thread terminates by returning from it's main function, as the documentation suggests. 如文档所示,通常最好是通过从线程的主函数中返回来终止线程。

One way of achieving this, and probably the easiest, is to throw some object that will be caught in the main thread function. 实现此目标(可能是最简单的方法)的一种方法是抛出一些将在主线程函数中捕获的对象。

For example: 例如:

struct ThreadEndingException { };

void DoSomeWork() {
    ...
    if (TestDestroy())
        throw ThreadEndingException();
    ...
}

void ThreadFunction() {
    try {
        DoSomeWork();
    }
    catch (const ThreadEndingException&) {
        // Do nothing, the function will return after leaving this catch.
    }
}

The current documentation for wxThread::Delete() says: wxThread :: Delete()的当前文档说:

This function works on a joinable thread but in that case makes the TestDestroy() function of the thread return true and then waits for its completion (ie it differs from Wait() because it asks the thread to terminate before waiting). 此功能在可连接线程,但在这种情况下,使螺纹的TestDestroy()函数返回true,然后为它完成等待(即脱离等待不同,(),因为它要求线程等待之前终止)。

So, it appears that you can use Delete() on a joinable thread. 因此,似乎可以在可连接线程上使用Delete()

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

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