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. Killing QThread inside of object. How?
Forum Updated to NodeBB v4.3 + New Features

Killing QThread inside of object. How?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 591 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!
    I've got an inheritance from previous developer where QThread objects is inside of root object. Something like this:

    class Class : public QObject {
    
    	Q_OBJECT
    
    	public:
    		
    		CustomObject* customObject1 = nullptr;
    		CustomObject* customObject1 = nullptr;
    
    		QThread* threadHighest;
    		QThread* threadHigh;
    		QThread* threadNormal;
    		QThread* threadLow;
    		QThread* threadLowest;
    

    At the class constructor:

    threadHighest = new QThread(this);
    threadHighest->start(QThread::Priority::HighestPriority);
    
    threadHigh = new QThread(this);
    threadHigh->start(QThread::Priority::HighPriority);
    
    threadNormal = new QThread(this);
    threadNormal->start(QThread::Priority::NormalPriority);
    
    threadLow = new QThread(this);
    threadLow->start(QThread::Priority::LowPriority);
    
    threadLowest = new QThread(this);
    threadLowest->start(QThread::Priority::LowestPriority);
    

    and this

    customObject1 = new CustomObject();
    cusomObject1->moveToThread(threadLow);
    
    customObject2 = new CustomObject();
    cusomObject2->moveToThread(threadHigh);
    

    The question is how to kill QThreads correctly at time of killing root object? For now all is working perfectly, but at time of exit of application happening crash.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested and https://doc.qt.io/qt-5/qthread.html#wait

      Why do you need all this threads?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        See https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested and https://doc.qt.io/qt-5/qthread.html#wait

        Why do you need all this threads?

        B Offline
        B Offline
        bogong
        wrote on last edited by
        #3

        @Christian-Ehrlicher I don't need. It's inheritance.

        Christian EhrlicherC 1 Reply Last reply
        0
        • B Offline
          B Offline
          bogong
          wrote on last edited by
          #4

          Issues closed. This works for me:

          threadLow->quit();
          threadLow->wait();
          

          or

          threadLow->exit();
          threadLow->wait();
          

          or

          threadLow->terminate();
          threadLow->wait();
          
          1 Reply Last reply
          1
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            The question is how to kill QThreads correctly at time of killing root object?

            Call quit() and then wait() for each of the threads. Before you do that signal those custom objects to destroy themselves in their respective threads..

            B 1 Reply Last reply
            4
            • Chris KawaC Chris Kawa

              The question is how to kill QThreads correctly at time of killing root object?

              Call quit() and then wait() for each of the threads. Before you do that signal those custom objects to destroy themselves in their respective threads..

              B Offline
              B Offline
              bogong
              wrote on last edited by
              #6

              @Chris-Kawa I've posted the same few seconds above you :-)))

              Chris KawaC 1 Reply Last reply
              0
              • B bogong

                @Christian-Ehrlicher I don't need. It's inheritance.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @bogong said in Killing QThread inside of object. How?:

                I don't need. It's inheritance.

                ?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • B bogong

                  @Chris-Kawa I've posted the same few seconds above you :-)))

                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @bogong I'm a slow typer I guess :) Good for you.

                  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