Important: Please read the Qt Code of Conduct -

Object Handles across QThread Boundaries

  • Banned

    First I am specifically using: Python 3.7, pyqt5, QThread

    I know I can do this because I have done it without the program crashing but I am wondering why this works since the documentation kind of says you cannot do it. Also I am aware that this would be basically not necessarily Thread Safe (and definitely not thread safe if done at more generic level) but then again I do not fully understand what the potential pitfalls are for this. So here is what I am doing and can anyone speak to it and outline what (if any) the actual issue(s) might be. I have not included code because this is more of a concept question but I will try to outline what I am doing as precisely as I can.

    I have created a Router Program its job is to spawn 3 non-generic threads (aka very specific purpose for each of these semi-permanet process threads that does not change over time) and that is all the threads it creates, which is to say, it never creates a bunch generic worker threads for anything. These three specific Threads are Database, Controller, and Listener.

    Now when I create these 3 Threads I can Emit a Signal from within the Thread that sends back to the Router a Handle to "Self" giving the Router via its Receiving Slot a Handle that gives it direct access to any and all functions within each of these sub-processes. With this Handle to its sub-processes the Router then can in-turn pass in a Handle to "Self" allowing each sub-process to have direct access to all of its functions.

    Now if I strictly limit any direct calls using these Handles to basically quick setter functions and such (aka very short lived functions that do a specific quick thing and are done such as setting a runtime flag to true or false) and use the Signal/Slots for sending stuff for the longer processing aspects (such as processing a data packet) so as not to lock the thread down due to a static connetion to another thread would there be any potential pitfalls that I am not aware of.

    Again I am slightly confused because it says you cannot send an "Object" across threads but it appears you can send a handle to that Object and reference it. Which is pretty close to the same thing without actually being the same thing. If that made any sense.

    Note in case anyone is curious and wants the why I would do this. In short Speed. As the direct access is, per what I have read, about 10 times faster than using a Signal/Slot

    So to reiterate, in a nutshell, would there be any issues to using a Handle to a Process-Object across Thread boundaries for quick actions while reserving the Signal/Slots for the longer actions.

  • @denni-0 said in Object Handles across QThread Boundaries:

    would there be any potential pitfalls that I am not aware of.

    well are breaking the rules that have been given. Just because it SEEMS to work, doesn't mean it is right. Remember that classes that are derived from QObject are not NORMAL, and are subject to whatever witchcraft the MOC system wielded.

  • Banned

    Okay I would point out that sometimes the rules are incorrect and/or there are loop-holes that can be used that work just fine and do not break its functionality. However these may be working because the QThread has been improperly set up -- for while I followed the documentation I have come to find out that it is actually incorrect in its presentation of how to implement a QThread -- so still trying to determine if this loop-hole is due to that improper documentation or if it is due to the nature of QThreads in general.

    So while I appreciate your feedback all it did was reiterate (in different words) what I was asking. Do you actually have any useful information about how QThreads actually work or is that the extent of your knowledge on that subject. Aka "Doc it hurts when I do this." Doctor: "Then do not do that" -- or technically correct but nearly useless type answer something you often get from Microsoft support assuming you get anything at all

  • Well then the frackin code. MOC does things to code that means you must follow their rules. If not, you produce questionable results...and using the historical QThread documentation debate isn't a good concrete example.

  • OK. probably seemed a bit terse. Here's the concern.

    If I read your post correctly, you have three QThread classes and want to call methods in those running thread classes directly. I think you have to keep in mind that the signal/slot mechanism is the means by which Qt guarantees thread safety. Can you call a getter method in the class on atomic data items directly? ... maybe...However, depending on the complexity of the calls you make directly to those running thread classes, your chance of introducing a race condition goes up if you don't use the signals/slots message IPC. That's what it's there for.

  • Banned

    Right but if Thread Safety is the only concerns then certain direct simplistic calls that get in adjust a flag and immediately get out ought not to create any issues especially if those kind of calls are mono-directional coming from one source and going to one destination with no cross-overs to any other threads basically in format I have a Main-Thread that talks to two child threads and these child-threads talk only to the Main-Thread sometimes setting certain flags and/or values is more easily done via a direct call while passing the more complex data is done via signal/slots --- and I want to be sure that doing this would not cause any hidden issues when it comes to how Threads work. I actually understand the under working mechanisms that might have been applied to threading (having actually written something like this in the past) but I am just not sure what methodology they implemented in doing this exactly.

    As for your RTFC -- sure I could -- like the masses -- not question how things work but frankly that is not my style and never has been -- and I have a lot of bugs in code because of my approach so I think I will keep it and not go with the rest of the lemmings as they plummet over that cliff ;) -- secondly I do not have the time to dig into the actual complete functionality of that code otherwise I probably would and I am just trying to find out if anyone else has already invented that wheel so I do not have to repeat making it. Its kind of like making modular code, I make it that way so I do not have to repeat making it for another project that needs something similar.

Log in to reply