[Qt-interest] Qthread takes a long time to stop
Scott Bloom
scott.bloom at onshorecs.com
Sat Jun 19 17:35:34 CEST 2010
You basiclly answered it yourself.
You need to shorten the getmessage and processMessage functions...
One very common technique, is inside the algorithms, query if they are
allowedToRun();
Ie..
HelperThread::getmessage()
{
if( !allowedToRun() ) return;
step1();
if( !allowedToRun() ) return;
step2();
if( !allowedToRun() ) return;
step3();
}
And the same for processMessage()..
But at some point you will get down to a 3rd party library call, which you
cant make any smaller... That will be the minimum wait time :(
Scott
On 6/19/10 8:02 AM, "Sarvesh Saran" <Sarvesh.Saran at synopsys.com> wrote:
> 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
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100619/a7f47e5c/attachment.html
More information about the Qt-interest-old
mailing list