2

I have the message "QWaitCondition: Destroyed while threads are still waiting" following the launch of N threads in a loop, and waiting for each in another loop.

Here is the code :

int nb_threads = QThread::idealThreadCount();
QFuture<void> futures[nb_threads];
bool shared_boolean;
// launch threads
for(int i = 0;i<nb_threads;++i){
    futures[i] = QtConcurrent::run(this,gpMainLoopMT,&shared_boolean,&next_pop_size,next_population);
}

// wait for threads to finish
for(int i = 0;i<nb_threads;++i){
    futures[i].waitForFinished();
}

I just can't figure out why this is happening, while I am waiting for each thread.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Samfaitmal
  • 80
  • 10
  • Just out of pure curiosity... how did you get 'int nb_threads = QThread::idealThreadCount(); QFuture futures[nb_threads];' to compile at all? – Greenflow May 08 '15 at 20:26
  • I see... GCC... I am using clang for too long. Normally it is forbidden to declare arrays with dynamic values. – Greenflow May 08 '15 at 20:33
  • The usage of QtConcurrent::run looks fine to me. The problem might be in gpMainLoopMT... whatever this function is. – Greenflow May 08 '15 at 21:29
  • Do you have an QApplication or QCoreApplication instance? https://forum.qt.io/topic/18462/qwaitcondition-destroyed-while-threads-are-still-waiting – fassl May 11 '15 at 19:58

2 Answers2

1

Actually I had the same warning when using Qt in a DLL. Windows kills all threads at application exit, before the DLL's global objects are destroyed. A global object destructor is where I was deleting the QApplication instance. This leads to an inconsistency because the QWaitConditions still think a thread is waiting, when in fact the native thread isn't running anymore, killed by Windows with no chance of proper cleanup. That's what leads to this warning. It's unfixable, even in Qt. Windows doesn't give us any chance to perform any cleanup, the threads just disappear.

David Faure
  • 1,836
  • 15
  • 19
0

You're not waiting for the threads, you're waiting for the tasks. The threads keep running until QApplication deletes the global QThreadPool instance. So the question is - are you leaking QApplication or destroying it properly?

David Faure
  • 1,836
  • 15
  • 19