Important: Please read the Qt Code of Conduct -

How to stop a QThread inmediately?

  • Hello,
    I have a Qthread running, and inside of this thread, there is a Qtcp connection.
    I call waitForReadyRead, then when the data is ready the thread proces the data. This steps are in a while loop. Now, if the user push a button on the gui ( A cancel transaction button), I need close the socket and terminate the thead.
    What is the correct way of do this with Qt and QThread?? in Pthread I have pthread_cancel, what is the equivalent in Qthread?? I already use QThread (SetTerminationEnabled and terminate) but this cause a crash program.

  • I had the same problem.
    and when CanFree (public boolean variable) is true the thread interrupts the loop and exit.

  • This is mi case, a litte code o mi program

    class Transaction:public QThread
    Transaction(QObject *parent):QThread(parent)
    //Any initialization of data

    void run()
    //Connection with serial port and socket connect (QtcpSocket.connectToHost()
    //QtcpSocket waitForConnect
    //Operation I/O blockink (Serial Port Read)
    //Data analice and generation dato for server
    //Operation blocking QtcpWrite (waitForBytesWritten
    //Operation blocking, QtcpSocket (waitForReadyRead)
    //Data analice and generate date for serial port or finished
    if( fin)
    //Operation I/O blocking (serial Port write)
    //Operation I/O blockink (Serial Port Read)



    class MainMenu:public QWidget
    Transaction transaction;
    //Show a screen waiting connect(ui.stopTransaction,SIGNAL(clicked()),this,SLOT(stop()));
    protected slots:
    void start()

    void start()


    under linux it works perfectly, under windows the application crash at runtime, the aplication dead. Whe append this secuencie.
    1- start
    2- stop while any operation blocking is apeend
    3- start
    The aplication crash in the call start() of metod QThread

  • This is my standard approach to QThread in all platforms:
    QThNetHTTPServerConn::QThNetHTTPServerConn() {
    CanFree= false;

    QThNetHTTPServerConn::~QThNetHTTPServerConn() {
    CanFree= true;
    while (this->isRunning()) msleep(10);

    void QThNetHTTPServerConn::run() {
    while (!CanFree) msleep(100);

    If you want to restart the same QThread please set CanFree= true; and then the QThread has ends the run function (use an event to know when) please run start() again.

  • But, it will not terminate inmediately. It will wait for all blocking operation on the socket and on the serial port until evaluate the condition. Consecuently the thread may be delay a lot of seconds.

  • Is not possible, in Qt as Delphi or Freepascal.
    As is not possible to not freeze the main form if you click on a button that runs WaitForreadyRead()

  • then, whats is the solution on Qt?? Is the last solution do the I/O operations non blocking, implementing a state maching??

  • I use often QThread. When I have to stop it, I normally wait till the thread is stopped....
    What are you doing exactly?

  • Terminate the thread in any point, everywhere it is not important.
    the problem is: I have only one hardware available, which is accesible by serial port, I want to stop the thread inmediately for leave the hardware free for other operation. Even if the user cancel the operation, and the operation in the serial port are blocking becouse it is more easy of managing.

  • It is a typical situation for me.
    In this case you have to verify frequently the CanFree variable in order to stop in less than a second and release the device.
    Why you have to use the same device with more software?

  • No, I use the same device with the same software, but in diferents situacions, and diferents operations.
    Maybe my English is no not enought precise, Sorry about that.

  • If you use the a device with only one program you have to think to the QThread as a component that obscures the device and works as provider for the program.
    You have to implement a public function SendCommand(), called from forms etc., the QThread has to run each operation and send a feedback to the program using signals.

Log in to reply