Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Thread refuses to quit
Qt 6.11 is out! See what's new in the release blog

Thread refuses to quit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tahayassen
    wrote on last edited by
    #1

    It is supposed to stop in myobject.cpp with QThread::currentThread()->quit(); but it doesn't.

    main.cpp:
    @#include <QCoreApplication>
    #include <QtCore>
    #include "myobject.h"

    QThread* cThread;
    MyObject* cObject;

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    cThread = new QThread();
    cObject = new MyObject();
    cObject->moveToThread(cThread);

    QObject::connect(cThread, SIGNAL(started()),
                     cObject, SLOT(doWork()));
    
    QObject::connect(cThread, SIGNAL(finished()),
                     cThread, SLOT(deleteLater()));
    
    QObject::connect(cThread, SIGNAL(finished()),
                     cObject, SLOT(deleteLater()));
    
    cThread->start();
    
    return a.exec&#40;&#41;;
    

    }@

    myobject.cpp:
    @#include "myobject.h"

    MyObject::MyObject(QObject *parent) :
    QObject(parent)
    {
    }

    void MyObject::doWork()
    {
    qDebug() << "Hi";
    QThread::currentThread()->quit(); // It is supposed to stop here, but it doesn't.
    for (int i = 0; i < 1000000; i++) {
    qDebug() << i;
    }
    }@

    myobject.h:
    @#ifndef MYOBJECT_H
    #define MYOBJECT_H

    #include <QtCore>

    class MyObject : public QObject
    {
    Q_OBJECT
    public:
    explicit MyObject(QObject *parent = 0);

    signals:

    public slots:
    void doWork();

    };

    #endif // MYOBJECT_H@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      bq. void QThread::quit() [slot]
      Tells the thread's event loop to exit with return code 0 (success). Equivalent to calling QThread::exit(0).

      Processing must return to the event loop before it can exit, which does not happen until your million debug messages are emitted.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        To clarify ChrisW67's point: Qt will not stop your thread in the middle of a function. If your thread got the "quit" command in the middle of doWork(), the thread will wait until doWork() returns before quitting.

        Terminating a thread in the middle of a function is dangerous and can cause data corruption.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved