PDA

View Full Version : can not stop QThread



newcfd
12th December 2015, 20:30
I am using Qthread/Worker in Qt4.8. In the worker, a third-party library is called.
The code worked fine before. Now qthread can not be terminated after data structure
is changed for the call to the third-party library. What can be the cause?

anda_skoa
13th December 2015, 12:38
There is not enough information to tell what the problem could be, but maybe the third party function does not return?

Cheers,
_

newcfd
13th December 2015, 20:33
it is standard Qthread/Worker model. 3rd party does not return anything. terminate is used to stop the qthread. But qthread keeps running until 3rd party code is finished.
The same code without any change works fine and Qthread stops quickly.

Added after 1 43 minutes:

the problem is found. I upgraded qt from 4.7.1.to 4.8.6. Qthread can stop quickly with 4.7.1, but not with 4.8.6.
I compared this with the same code. Any ideas?

anda_skoa
13th December 2015, 23:23
3rd party does not return anything.

I was not asking what it returns, I was asking it it returns.

When you call QThread::quit() to end the thread's event loop, it will do so once the 3rd party code that it currently executes returns.

You can think of this as setting a flag, which the thread will check and react on at its first possible opportunity.
As long as it is executing 3rd party code it is not yet in that situation.

So one possible cause for the thread not ending when you expect it to end is that it is still executing the 3rd party code and has not reached the check yet.

Always remember that a thread needs to cooperate when being told to stop as there is no safe way of terminating it like one could terminate a process.

Cheers,
_

newcfd
14th December 2015, 00:51
Thanks for your reply. I got what you wrote. There is no flag in the third party.
If Qt4.8.6 is used, the third party does not return right away and keeps running instead
after Qthread.terminate() is called . But with 4.7.1, the thread stops right away and
obviously the third party returns immediately.

anda_skoa
14th December 2015, 07:59
Oh, you are literally calling QThread::terminate().

That is a very, very bad idea if you want your program to continue afterwards.

Terminating a thread like that leaves your application in an unknown state!
Any memory allocated by the thread up to this point will be leaked, locks will be held indefinitely, etc.

The only safe way of preemptively ending a parallel execution context is to put it into a helper program and run it as a child process.
Process resources, such as memory, are tracked by the operating system and can thus be released upon termination.

Cheers,
_

newcfd
14th December 2015, 19:43
Thanks for your explanation. I know this problem.
It looks like the third party code can handle this issue.

Now the question is
why QThread in 4.8.6 does not work properly while QThread in 4.7.1 is fine
in my application? The code is exactly same.

anda_skoa
15th December 2015, 11:03
Have you tried terminating an "empty" thread?
Or a thread that just runs a loop with some sleep in it?

Cheers,
_