QtConcurrent::run() causes QWaitCondition destroyed messages
-
I’m trying to run a function of a class using the QtConcurrent::run() method. The thread runs and does exactly what it should. However, when the thread ends, I get this error message in the console:
@QWaitCondition: Destroyed while threads are still waiting@
It works fine on Linux without any error message, but application crashes in Windows when i close the application
Here is the code of main function:
@ int main(int argc, char *argv[])
{
QtSingleApplication a(argc, argv);if(a.isRunning()&&argc==2) { a.sendMessage(argv[1]); return 0; } else if (a.isRunning()) { return 0; } testApp w; testApp.show(); a.setQuitOnLastWindowClosed(false); if(argc==2) { w.showDialog(argv[1]); } QObject::connect(&a, SIGNAL(messageReceived(const QString&)), &w, SLOT(showDialog(QString))); return a.exec(); }
@
-
Probably need to see more of the code to figure out what is going on. How do you create your thread? I am assuming your thread gets generated in your testApp class.
What is it saying is that your thread is not being cleaned up or killed properly before your 'parent' class is being deleted. You have a wait condition that is still probably being hit while your main is trying to return from its run loop and delete all of its children (the thread).
If your in a run loop of some sort, call exit() on the thread class before you clean everything up in your 'parent' class. Or pop out of your while in your run() function within the thread (many ways to do this depending on how your using the thread) and back out of the wait condition.
-
Thanks for the reply. Here is the code:
@void MyClass::ready()
{
QtConcurrent::run (this,&MyClass::start);
}void MyClass::start()
{// do something
emit
done();
}@After, the done() signal is emitted, if i close the application, the application crashes, but if i wait for half a minute or so, and then close the application, it works fine.
I just want to run a single function of class in a separate thread. -
Just because you emit a done() signal does not mean that the thread has been closed properly.
What you will probably need to do is to use the return value of QtConcurrent:run (which is a QFuture class) and check for QFuture:result(). QFuture:result() will block until the thread is done. You could also use QFuture::waitForFinished if you don't have a return value.
Forget the "signal when thread is done" method, this is a really dangerous approach. -
-
No, that's the wrong place. With your current code the thread waits for its own execution to be finished (what is not very likely, as it is busy with waiting for its own execution to be finished ;-D )
@ void MyClass::ready()
{
future = QtConcurrent::run (this,&MyClass::start);
future.waitForFinished() // maybe do this somewhere outside (in your main class)
}void MyClass::start() { // do what needs to be done }@