What is the use of QThread.wait() function?
c++, multithreading, qt
Solution
I finally found my answer here: http://comments.gmane.org/gmane.comp.lib.qt.user/6090
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.
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();
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?
Problem
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. 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. 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. Am I missing something? 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; } ```