Qt-interest Archive, December 2006
connect qApp's aboutToQuit() signal to QThread::quit()
Message 1 in thread
Using Qt-4.2.2 on RHEL 3.
In my QThread's run() function, just before calling exec(), I do this:
connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(quit()) );
I expected this to call quit() on my thread to avoid the following
warning message at shut down:
QThread: Destroyed while thread is still running.
I've tried with both Qt::DirectConnection and Qt::QueuedConnection with
the same luck. If I connect the signal to QThread::terminate() instead
of QThread::quit(), I avoid the warning message, but it gives me
gastrointestinal discomfort. And no, my QThread's event loop is not busy
at the time the qApp gets shut down.
Any ideas what's going wrong? Thanks in advance!
--Dave
--
[ signature omitted ]
Message 2 in thread
Dave Smith wrote:
> Using Qt-4.2.2 on RHEL 3.
>
> In my QThread's run() function, just before calling exec(), I do this:
>
> connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(quit()) );
>
> I expected this to call quit() on my thread to avoid the following
> warning message at shut down:
>
> QThread: Destroyed while thread is still running.
>
> I've tried with both Qt::DirectConnection and Qt::QueuedConnection with
> the same luck. If I connect the signal to QThread::terminate() instead
> of QThread::quit(), I avoid the warning message, but it gives me
> gastrointestinal discomfort. And no, my QThread's event loop is not busy
> at the time the qApp gets shut down.
>
> Any ideas what's going wrong? Thanks in advance!
What you want is terminate()
/eno
--
[ signature omitted ]
Message 3 in thread
troll@xxxxxxxxxxxx wrote:
> Dave Smith wrote:
>> Using Qt-4.2.2 on RHEL 3.
>>
>> In my QThread's run() function, just before calling exec(), I do this:
>>
>> connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(quit()) );
>>
>> I expected this to call quit() on my thread to avoid the following
>> warning message at shut down:
>>
>> QThread: Destroyed while thread is still running.
>>
>> I've tried with both Qt::DirectConnection and Qt::QueuedConnection
>> with the same luck. If I connect the signal to QThread::terminate()
>> instead of QThread::quit(), I avoid the warning message, but it gives
>> me gastrointestinal discomfort. And no, my QThread's event loop is
>> not busy at the time the qApp gets shut down.
>>
>> Any ideas what's going wrong? Thanks in advance!
>
> What you want is terminate()
According to the Qt docs, it's not good practice to use
QThread::terminate(), hence my comment about gastrointestinal discomfort.
--Dave
--
[ signature omitted ]
Message 4 in thread
Dave Smith wrote:
> troll@xxxxxxxxxxxx wrote:
>> Dave Smith wrote:
>>> Using Qt-4.2.2 on RHEL 3.
>>>
>>> In my QThread's run() function, just before calling exec(), I do this:
>>>
>>> connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(quit()) );
>>>
>>> I expected this to call quit() on my thread to avoid the following
>>> warning message at shut down:
>>>
>>> QThread: Destroyed while thread is still running.
>>>
>>> I've tried with both Qt::DirectConnection and Qt::QueuedConnection
>>> with the same luck. If I connect the signal to QThread::terminate()
>>> instead of QThread::quit(), I avoid the warning message, but it gives
>>> me gastrointestinal discomfort. And no, my QThread's event loop is
>>> not busy at the time the qApp gets shut down.
>>>
>>> Any ideas what's going wrong? Thanks in advance!
>>
>> What you want is terminate()
>
> According to the Qt docs, it's not good practice to use
> QThread::terminate(), hence my comment about gastrointestinal discomfort.
Sorry, I missed that :-(
Well, I can understand your discomfort. However, if you connect
aboutToQuit() with quit() does not guarantee, that your threads' event
loops even get a chance to run and then to quit properly, which in turn
lets these threads survive.
I see these options:
(a)
after the quit() slots are invoked you put your app's main thread to
sleep for a while - but synchronisation based upon sleeps is a bad
thing(tm)
(b)
after the quit() slots are invoked you put you QThread::wait() for the
your threads. This is the cleanest solution to your problem, but what do
you do if one of your threads hangs?
(c)
or you just terminate() them. You are right, QThread::terminate() is
evil (and for that reason there is no equivalent to that in Java, for
example) because it works similar to killing an application: your thread
gets no chance to clean up etc. pp.
May be a combination of (b) and (c) works best in that your threads can
clean up properly, still your application does not hang on shutdown.
/eno
--
[ signature omitted ]