Problems with Open-Source Downloads read and

Does shared memory access from different QThreads behave similarly to native python threads?

  • Hi. I don't really have experience with threads and I'm currently trying to separate some parts of a PySide based software into different threads.
    AFAIK, a standard threading.Thread object created in the main thread shares the memory with the main thread which enables direct access and modification of data from both sides. Just to make sure, I want to ask, if this still holds for QThreads in all cases.
    If I instanciate some object a of class A(QObject) in a custom QThread, for instance, could I savely access and modify data (attributes) of a from the main thread (assuming I got a reference to a through a signal or sth)?

  • Lifetime Qt Champion


    Unless the class is specifically designed to be thread safe, it's never safe to access them without proper protection in place.

  • @SGaist Ah yes, if I have more threads as in the example above that access the same data, I need to make sure that this does not happen at the same time. In this case I would need to add some locking mechanism probably (never did this but I have about it, it shouldn't be an issue in my application). Is this what you mean?
    I was just wondering if there is some conceptual restriction so that it's always illegal to access data of another QThread directly.

  • @nutrx
    Yes, conceptually you are allowed to access the shared data across threads.

    From Qt you would typically use QMutex/QMutexLocker to synchronise safe access.

    I don't know, but it may be that Python objects across threads has this protection built in. But certainly from C++ you would need to put in mutexes, or some other scheme.

  • @JonB thank you!

Log in to reply