Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Does shared memory access from different QThreads behave similarly to native python threads?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 896 Views 1 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.
  • N Offline
    N Offline
    nutrx
    wrote on last edited by
    #1

    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)?
    Thanks!

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

      Hi,

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

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

      N 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

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

        N Offline
        N Offline
        nutrx
        wrote on last edited by nutrx
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • N nutrx

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          N 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            N Offline
            N Offline
            nutrx
            wrote on last edited by
            #5

            @JonB thank you!

            1 Reply Last reply
            0

            • Login

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