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 ownership of object when subclassing QThread
Forum Updated to NodeBB v4.3 + New Features

Thread ownership of object when subclassing QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.6k Views 2 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.
  • K kengineer

    If I have a custom class in which I subclasses QThread (and thus I implement run() ), how can I pass ownership of one of the class members to the new thread?

    i.e.

    class SomeClass : QThread
    {
       SomeClass() {
          start();
       }
    
       void run();
    
       QSerialPort m_serial_port;
    }
    

    Is this case, how could I change the ownership of m_serial_port from the base thread in which SomeClass is created to the new thread created when start is called? That way I can use m_serial_port in the context of that thread and within any other private functions that are called from it.

    I have seen there are functions for moving objects to a thread, but those examples are when a QThread is created as a separate object and not subclassed (as the thread handle is readily available then).

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    @kengineer
    before we go along this path, let me ask, why do you want to subclass qthread, in the first place?


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    K 1 Reply Last reply
    3
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi,

      If you re-implement run, create m_serial_port in the body of the run method. That way, it will be in the new thread.

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

      1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        @kengineer
        before we go along this path, let me ask, why do you want to subclass qthread, in the first place?

        K Offline
        K Offline
        kengineer
        wrote on last edited by
        #4

        @J.Hilk said in Thread ownership of object when subclassing QThread:

        @kengineer
        before we go along this path, let me ask, why do you want to subclass qthread, in the first place?

        Well it doesn't have to be that way, I thought about switching it if there's not a way to easily to what I'm asking (without passing around references at runtime or something), it's just a valid and simple way to create a thread for a class.

        @SGaist said in Thread ownership of object when subclassing QThread:

        Hi,

        If you re-implement run, create m_serial_port in the body of the run method. That way, it will be in the new thread.

        Yes I know I can do that, but then if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class.

        kshegunovK J.HilkJ 2 Replies Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          What function ? How are you planning to use it ?

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

          1 Reply Last reply
          0
          • K kengineer

            @J.Hilk said in Thread ownership of object when subclassing QThread:

            @kengineer
            before we go along this path, let me ask, why do you want to subclass qthread, in the first place?

            Well it doesn't have to be that way, I thought about switching it if there's not a way to easily to what I'm asking (without passing around references at runtime or something), it's just a valid and simple way to create a thread for a class.

            @SGaist said in Thread ownership of object when subclassing QThread:

            Hi,

            If you re-implement run, create m_serial_port in the body of the run method. That way, it will be in the new thread.

            Yes I know I can do that, but then if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #6

            @kengineer said in Thread ownership of object when subclassing QThread:

            if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class

            Trouble is brewing here. Don't do that, use signal-slot connections.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            4
            • K kengineer

              @J.Hilk said in Thread ownership of object when subclassing QThread:

              @kengineer
              before we go along this path, let me ask, why do you want to subclass qthread, in the first place?

              Well it doesn't have to be that way, I thought about switching it if there's not a way to easily to what I'm asking (without passing around references at runtime or something), it's just a valid and simple way to create a thread for a class.

              @SGaist said in Thread ownership of object when subclassing QThread:

              Hi,

              If you re-implement run, create m_serial_port in the body of the run method. That way, it will be in the new thread.

              Yes I know I can do that, but then if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @kengineer
              let me quote myself from earlier today:

              take a look here
              https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
              and here
              https://doc.qt.io/qt-5/qthread.html#details

              on how to properly do threading in Qt.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              4
              • K Offline
                K Offline
                kengineer
                wrote on last edited by
                #8

                @SGaist said in Thread ownership of object when subclassing QThread:

                What function ? How are you planning to use it ?

                Creating some other functions that do different tasks related to the serial port within my thread. I understand that there are other options on how to handle this, but my original question remains about moving the object to thread.

                @kshegunov said in Thread ownership of object when subclassing QThread:

                @kengineer said in Thread ownership of object when subclassing QThread:

                if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class

                Trouble is brewing here. Don't do that, use signal-slot connections.

                Race condition how? I'm not planning on accessing the serial port object outside of the context of the newly created thread, I just desire to use it within other private helper functions I create in my class that are only used within the new thread. If I was then yes I should use signals and slots.

                From what I gather so far it sounds like there's not a simple way to do this or rather I should create a new independent class with the port and then create a thread object manually rather than subclassing.

                kshegunovK jsulmJ 2 Replies Last reply
                0
                • K kengineer

                  @SGaist said in Thread ownership of object when subclassing QThread:

                  What function ? How are you planning to use it ?

                  Creating some other functions that do different tasks related to the serial port within my thread. I understand that there are other options on how to handle this, but my original question remains about moving the object to thread.

                  @kshegunov said in Thread ownership of object when subclassing QThread:

                  @kengineer said in Thread ownership of object when subclassing QThread:

                  if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class

                  Trouble is brewing here. Don't do that, use signal-slot connections.

                  Race condition how? I'm not planning on accessing the serial port object outside of the context of the newly created thread, I just desire to use it within other private helper functions I create in my class that are only used within the new thread. If I was then yes I should use signals and slots.

                  From what I gather so far it sounds like there's not a simple way to do this or rather I should create a new independent class with the port and then create a thread object manually rather than subclassing.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #9

                  @kengineer said in Thread ownership of object when subclassing QThread:

                  Race condition how? I'm not planning on accessing the serial port object outside of the context of the newly created thread, I just desire to use it within other private helper functions I create in my class that are only used within the new thread. If I was then yes I should use signals and slots.

                  QThread is not a thread, it never was and is not ever going to be. The thread is the QThread::run function, nothing more, nothing less - a root of a stack. Anything you put in your QThread class can be used from the thread that created that QThread instance, everything else is abusing it and begging for trouble. I mean you can do it if you know what you're doing, but it's not good, and you run the risk of shooting yourself in the foot.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  6
                  • K kengineer

                    @SGaist said in Thread ownership of object when subclassing QThread:

                    What function ? How are you planning to use it ?

                    Creating some other functions that do different tasks related to the serial port within my thread. I understand that there are other options on how to handle this, but my original question remains about moving the object to thread.

                    @kshegunov said in Thread ownership of object when subclassing QThread:

                    @kengineer said in Thread ownership of object when subclassing QThread:

                    if I want to use the object in other functions of my class then I need to constantly pass it around, rather than just referencing a private member of my class

                    Trouble is brewing here. Don't do that, use signal-slot connections.

                    Race condition how? I'm not planning on accessing the serial port object outside of the context of the newly created thread, I just desire to use it within other private helper functions I create in my class that are only used within the new thread. If I was then yes I should use signals and slots.

                    From what I gather so far it sounds like there's not a simple way to do this or rather I should create a new independent class with the port and then create a thread object manually rather than subclassing.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @kengineer You can declare m_serial_port as pointer in your class and new it inside run(), then there is no need to pass it around (but make sure you new it BEFORE it is accessed for the first time).

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • K Offline
                      K Offline
                      kengineer
                      wrote on last edited by
                      #11

                      Thanks for all the feedback guys. Based on some of the links provided, it seems like maybe I need to adjust the way I am using threads and this exercise won't really be necessary at all. I find it interesting that even after all these years the Qt docs still mention the subclass thing as an example when this isn't really a recommended way to do it.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        You should also read QThread, you were not doing so wrong.

                        As the latest version of the doc explains, both use case are valid. But you have to understand the subtitles between them. And indeed, most of the time, the worker object is the simplest.

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

                        1 Reply Last reply
                        4

                        • Login

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