The following code is called by a subclass of QThread in the cancel method. I need to wait for the thread to finish, but I can't use QThread::wait() because I must not block the main UI thread. Following the advice here, I tried to use a QEventLoop.
Qt Code:
  1. QEventLoop eventLoop;
  2. connect(this, SIGNAL(finished()), &eventLoop, SLOT(quit()));
  3. // Race condition
  4. if (!isFinished())
  5. eventLoop.exec();
To copy to clipboard, switch view to plain text mode 
The problem is that the thread might emit the finished() signal after the call to isFinished(), but before the call to exec(). Unfortunately, exec() will not exit in this case. How can I prevent the dead lock if I don't want to add a timeout?