title: What is the use of QThread.wait() function?
body: <p>I have stumbled upon this problem, as others haves:
  <a href="https://stackoverflow.com/questions/7432861/qthread-wont-stop-does-not-process-a-signal">QThread won&#39;t stop / does not process a signal</a>
  <a href="https://stackoverflow.com/questions/5790514/qthread-using-a-slot-quit-to-exit-the-thread">QThread - Using a slot quit() to exit the thread</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Am I missing something?</p>
  
  <p>I thought I need to add a code snippet:</p>
  
  <pre><code>for (auto i = myVector.begin(); i &lt; myVector.end(); ++i)
  {
  
      // 5-line best practice creation for the thread
      QThread*  workerThread       = new QThread;
      MyWorkerObject* workerObject = new MyWorkerObject(0);
      workerObject-&gt;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-&gt;start();
  
      // Invoking the work
     QMetaObject::invokeMethod(workerObject, "StartYourJob", Qt::QueuedConnection, Q_ARG(SomeType, *i));
  
      // Synchronization   
     workerThread-&gt;wait();
     delete wokerThread;
  }
  </code></pre>