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. What does it mean by "thread owns a QObject"?
QtWS25 Last Chance

What does it mean by "thread owns a QObject"?

Scheduled Pinned Locked Moved Unsolved General and Desktop
event loopqobjectqthread
9 Posts 3 Posters 1.0k Views
  • 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.
  • H Offline
    H Offline
    haccks
    wrote on 26 Sept 2023, 17:23 last edited by haccks
    #1

    What does it mean by "thread owns a QObject"? I found this in the doc

    Calling delete on a QObject from a thread other than the one that owns the object (or accessing the object in other ways) is unsafe [...]
    By default, the thread that owns a QObject is the thread that creates the QObject, but not after QObject::moveToThread() has been called.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 Sept 2023, 17:30 last edited by
      #2

      Hi and welcome to devnet,

      Exactly what it said in the documentation you quoted: the thread in which you create the object owns it. You have always at least one thread in your application. You can create more if needed and thus you have to take good care on where and when the objects are created to ensure they are owned by the expected thread or are moved to it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply 26 Sept 2023, 17:35
      2
      • S SGaist
        26 Sept 2023, 17:30

        Hi and welcome to devnet,

        Exactly what it said in the documentation you quoted: the thread in which you create the object owns it. You have always at least one thread in your application. You can create more if needed and thus you have to take good care on where and when the objects are created to ensure they are owned by the expected thread or are moved to it.

        H Offline
        H Offline
        haccks
        wrote on 26 Sept 2023, 17:35 last edited by haccks
        #3

        @SGaist Is it related to thread affinity? These two together confuses me. QObject::moveToThread() changes thread affinity and also the ownership of the object. Are they same?

        P 1 Reply Last reply 27 Sept 2023, 02:03
        0
        • H haccks
          26 Sept 2023, 17:35

          @SGaist Is it related to thread affinity? These two together confuses me. QObject::moveToThread() changes thread affinity and also the ownership of the object. Are they same?

          P Offline
          P Offline
          Pl45m4
          wrote on 27 Sept 2023, 02:03 last edited by Pl45m4
          #4

          @haccks said in What does it mean by "thread owns a QObject"?:

          QObject::moveToThread() changes thread affinity and also the ownership of the object. Are they same?

          In this case, yes. As @SGaist said above, the thread where the QObject was created in, owns this object. If you move it to thread X, the ownership is also moved. The moved QObject "lives" in thread X now.

          You can check the affinity of every QObject by calling

          • https://doc.qt.io/qt-6/qobject.html#thread

          Like

          QThread* myThread = new QThread();
          
          QObject* obj = new QObject();
          
          // obj in main thread where it was created
          if(obj->thread() == this->thread())
                qDebug() << "Obj in Main thread";
          
          
          obj->moveToThread(myThread);
          
          // after moving, ownership went to "myThread"
          if(obj->thread() == myThread())
                qDebug() << "Obj in myThread";
          
          // ...
          // "Worker" approach:
          // connect signals for thread control
          // and start myThread afterwards
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          H 1 Reply Last reply 27 Sept 2023, 02:25
          1
          • P Pl45m4
            27 Sept 2023, 02:03

            @haccks said in What does it mean by "thread owns a QObject"?:

            QObject::moveToThread() changes thread affinity and also the ownership of the object. Are they same?

            In this case, yes. As @SGaist said above, the thread where the QObject was created in, owns this object. If you move it to thread X, the ownership is also moved. The moved QObject "lives" in thread X now.

            You can check the affinity of every QObject by calling

            • https://doc.qt.io/qt-6/qobject.html#thread

            Like

            QThread* myThread = new QThread();
            
            QObject* obj = new QObject();
            
            // obj in main thread where it was created
            if(obj->thread() == this->thread())
                  qDebug() << "Obj in Main thread";
            
            
            obj->moveToThread(myThread);
            
            // after moving, ownership went to "myThread"
            if(obj->thread() == myThread())
                  qDebug() << "Obj in myThread";
            
            // ...
            // "Worker" approach:
            // connect signals for thread control
            // and start myThread afterwards
            
            H Offline
            H Offline
            haccks
            wrote on 27 Sept 2023, 02:25 last edited by haccks
            #5

            @Pl45m4 AFAIU, a QObject living in thread means this thread will handle its queued signals/events provided the thread has it's own event loop. This is what it meant by "a thread owns a QObject?

            P 1 Reply Last reply 27 Sept 2023, 04:30
            0
            • H haccks
              27 Sept 2023, 02:25

              @Pl45m4 AFAIU, a QObject living in thread means this thread will handle its queued signals/events provided the thread has it's own event loop. This is what it meant by "a thread owns a QObject?

              P Offline
              P Offline
              Pl45m4
              wrote on 27 Sept 2023, 04:30 last edited by
              #6

              @haccks said in What does it mean by "thread owns a QObject"?:

              a QObject living in thread means this thread will handle its queued signals/events

              Yes, or in other words, the slot/function will run in the thread where the receiver QObject lives in.
              Therefore you should also always use Qt::QueuedConnection in a connect statement, when dealing with more than one thread (or let Qt decide, which works best in most cases). If you force a Qt::DirectConnection across threads, the function will run in the calling thread and cause unwanted behavior or nothing happens at all.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              H 1 Reply Last reply 27 Sept 2023, 09:29
              2
              • P Pl45m4
                27 Sept 2023, 04:30

                @haccks said in What does it mean by "thread owns a QObject"?:

                a QObject living in thread means this thread will handle its queued signals/events

                Yes, or in other words, the slot/function will run in the thread where the receiver QObject lives in.
                Therefore you should also always use Qt::QueuedConnection in a connect statement, when dealing with more than one thread (or let Qt decide, which works best in most cases). If you force a Qt::DirectConnection across threads, the function will run in the calling thread and cause unwanted behavior or nothing happens at all.

                H Offline
                H Offline
                haccks
                wrote on 27 Sept 2023, 09:29 last edited by
                #7

                @Pl45m4 Thank you! One last thing, when a QObject is moved to another thread then it can still be used/accessed in the thread where it is declared?

                P 1 Reply Last reply 27 Sept 2023, 11:53
                0
                • H haccks
                  27 Sept 2023, 09:29

                  @Pl45m4 Thank you! One last thing, when a QObject is moved to another thread then it can still be used/accessed in the thread where it is declared?

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 27 Sept 2023, 11:53 last edited by
                  #8

                  @haccks said in What does it mean by "thread owns a QObject"?:

                  it can still be used/accessed in the thread where it is declared?

                  Define "used"...
                  In general, as long as the pointer is valid, it works but you should not call functions directly. Use signals to communicate instead.

                  https://doc.qt.io/qt-6/qthread.html#details


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  H 1 Reply Last reply 27 Sept 2023, 13:06
                  0
                  • P Pl45m4
                    27 Sept 2023, 11:53

                    @haccks said in What does it mean by "thread owns a QObject"?:

                    it can still be used/accessed in the thread where it is declared?

                    Define "used"...
                    In general, as long as the pointer is valid, it works but you should not call functions directly. Use signals to communicate instead.

                    https://doc.qt.io/qt-6/qthread.html#details

                    H Offline
                    H Offline
                    haccks
                    wrote on 27 Sept 2023, 13:06 last edited by
                    #9

                    @Pl45m4 Calling slots as a normal function directly from main thread, reading/writing from/to a variable of QObject (using mutex of course).

                    1 Reply Last reply
                    0

                    1/9

                    26 Sept 2023, 17:23

                    • Login

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