[Qt-interest] Qthread takes a long time to stop

Sarvesh Saran Sarvesh.Saran at synopsys.com
Sat Jun 19 17:02:57 CEST 2010


Hi,

I have a helper thread derived from QThread which runs in a forever loop. Inside the loop it reads messages from a server , does some processing of the messages and send the message to the Main GUI thread.

So the helper thread looks something like:

Helperthread::run()
{
                while(allowedToRun())
                {
                                QString msg;
getmessage(&msg); //read a message from the server
processMessage(msg);
                }
}

Helperthread::processMessage()
{
//lots of case statements here....(to identify message types) and lots of message processing
}

//this is a public slot called by the main GUI thread
Helperthread::setAllowedToRun()
{
//set the flag to false;
}

My problem is that when the main window gets a close signal, it sets the flag to false and has to wait for a long time for the helper thread to finish.

i.e in the main window on close :

MainWindow::closeEvent()
{
helperthread->setAllowedToRun(false);
helperthread->wait();  //this takes a long time because the helper thread's processMessage is quiet big and takes a long while to return.
}

Is there a way I can speed this up so that my main window does not have to wait for a long time for the helper thread to exit ?


Thanks,
Sarvesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100619/b3ac8e90/attachment.html 


More information about the Qt-interest-old mailing list