Unsolved 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.
-
@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.
-
What function ? How are you planning to use it ?
-
@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.
-
@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#detailson how to properly do threading in Qt.
-
@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.
-
@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 theQThread::run
function, nothing more, nothing less - a root of a stack. Anything you put in yourQThread
class can be used from the thread that created thatQThread
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. -
@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).
-
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.
-
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.