[Qt-interest] Proper way to terminate QThreads

BRM bm_witness at yahoo.com
Tue Nov 17 18:19:20 CET 2009


I have several programs in which I am using QThread derived objects to do some working, mostly handling network connections. The basic structure is as follows:

myThread - derived from QThread
myThreadInstance - class that does the work; instantiated via myThread::run()

myThread is pretty much just a signals, with myThreadInstance having signals and slots.
The main program starts the thread using myThread->start().

I am presently having a small issue with one program that handles network connections using this threading setup - I receive a network connection from a QTcpServer instance and hand it off to a thread to manage. However, I need to cancel the thread when the connection goes away after I notify the main thread (so that it can initiate the cancel).

What is the proper way to cancel the thread?

I've noticed in the documentation that there is QThread::quit() and QThread::terminate(); however, I thought I remember reading somewhere that those should only be called from within the thread being terminated - not from a parent thread.
What's the proper method for stopping these threads?

Thanks!

Ben




More information about the Qt-interest-old mailing list