QThread.wait()函数的用途是什么? [英] What is the use of QThread.wait() function?

查看:1715
本文介绍了QThread.wait()函数的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像其他人一样偶然发现了这个问题: QThread不会停止/不处理信号 QThread-使用插槽quit()退出线程

I have stumbled upon this problem, as others haves: QThread won't stop / does not process a signal QThread - Using a slot quit() to exit the thread

问题是我想启动一个工作线程,执行一些工作(这涉及将信号发送到代码中的其他线程,然后异步接收信号),然后退出.但是我希望此线程与启动它的代码同步.换句话说,我希望在创建工作线程之前停止执行该代码,直到工作线程完成其工作为止.

The problem is that I want to have a worker thread started, do some job (which involves sending signals to other threads in my code, and receiving signals asynchronously) and then exit. But I want this thread to be synchronized with the code that is starting it. In other words, I want the execution in the code which creates the worker thread to be halted until the worker thread is done its job.

但是看来这在Qt中是不可能的.原因是无法从线程本身内发出工作程序的QThread.quit()插槽信号.侦听此插槽信号的事件循环应位于创建工作线程的同一线程中.这意味着不应阻塞正在创建的线程,否则工作线程将永远不会停止.

But it seems this is not possible in Qt. The reason is that the worker's QThread.quit() slot cannot be signaled from within the thread itself. The event loop which listens for signals to this slot, should reside in the same thread that created the worker thread. This means the creating thread should not be blocked, otherwise the worker thread never stops.

这带给我一个问题,那QThread.wait()的意义何在?我认为此函数应仅停留在程序的末尾,以确保所有线程均已退出,但实际上不能将其用于同步线程,至少不能将其与创建的线程同步.它.因为如果从创建线程中调用QThread.wait(),它将阻止其事件循环,这将阻止工作线程的接口,从而阻止其退出.

Which brings me to my question, that what is the point of QThread.wait() then? I think this function should just be stuck at the end of the program to make sure all the threads have exited, but it cannot actually be used to synchronize threads, at least it cannot be used to synchronize a worker thread, with the thread that created it. Because if the QThread.wait() is called from the creating thread, it blocks its event loop, which will block the worker thread's interface, which will prevent it from ever exiting.

我想念什么吗?

我认为我需要添加一个代码段:

I thought I need to add a code snippet:

for (auto i = myVector.begin(); i < myVector.end(); ++i)
{

    // 5-line best practice creation for the thread
    QThread*  workerThread       = new QThread;
    MyWorkerObject* workerObject = new MyWorkerObject(0);
    workerObject->moveToThread(workerThread);
    QObject::connect(workerThread, SIGNAL(started()), workerObject, SLOT(init()));
    QObject::connect(workerThread, SIGNAL(finished()), workerObject, SLOT(deleteLater()));  

    // Stop mechanism
    QObject::connect(workerObject, SIGNAL(finished()), workerThread, SLOT(quit()));

    // Start mechanism
    wokerThread->start();

    // Invoking the work
   QMetaObject::invokeMethod(workerObject, "StartYourJob", Qt::QueuedConnection, Q_ARG(SomeType, *i));

    // Synchronization   
   workerThread->wait();
   delete wokerThread;
}

推荐答案

我终于在这里找到了答案: http://comments.gmane.org/gmane.comp.lib.qt .user/6090

I finally found my answer here: http://comments.gmane.org/gmane.comp.lib.qt.user/6090

简而言之,如果将QThread :: quit()作为插槽调用,则创建线程的事件循环处理程序将对其进行处理,这不是我想要的.

In short, if QThread::quit() is invoked as a slot, the event loop handler of the creating thread will deal with it, which is not what I want.

我应该直接叫它.因此,当workerObject完成其工作时,它应该直接调用其容器的quit,而不是发送信号(该信号必须通过被阻止的创建线程):

I should call it directly. So when the workerObject finishes its job, instead of sending a signal (which has to pass through the blocked creating thread), it should directly call its container's quit:

this->thread()->quit();

这将是workerObject的出口.现在不再需要停止机制,并且可以从代码中消除这些行.

This would be the exit point of the workerObject. Now there is no need for the stop mechanism and these lines can be eliminated from the code.

// Stop mechanism
QObject::connect(workerObject, SIGNAL(finished()), workerThread, SLOT(quit()));

有人认为这种方法有什么问题吗?

Does anybody see any problem with this approach?

这篇关于QThread.wait()函数的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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