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. Calling a function on an QObject subclass that doesn't live in the current thread
Forum Updated to NodeBB v4.3 + New Features

Calling a function on an QObject subclass that doesn't live in the current thread

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 272 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.
  • A Offline
    A Offline
    andyP
    wrote on last edited by andyP
    #1

    According to the documentation :
    "If you are calling a function on an QObject subclass that doesn't live in the current thread and the object might receive events, you must protect all access to your QObject subclass's internal data with a mutex; otherwise, you may experience crashes or other undesired behavior."
    Consider the following class:

    // Created on stack in main() => thread affinity = main thread
    class Model : public QObject {
        Q_OBJECT
    public:
       int getValue() const { // will be called from both main thread and non main thread
            return m_value;
       };
    public slots:
        void incrementValue() { // will be called from both main thread and non main thread
            m_value++; 
        };
    private:
        std::atomic<int> m_value = 0;
    }
    

    What is the internal data of Model?

    std::atomic<int> m_value clearly is internal data.
    It is already atomic and should not need to be protected by a mutex?

    But what about the code generated by the Q_OBJECT macro?
    Must I somehow synchronize access to data here as well? If so how?

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

      Hi,

      In your class, there's nothing more to do.

      As stated in the documentation, it's your internal data access that you must protect.

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

      A 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        In your class, there's nothing more to do.

        As stated in the documentation, it's your internal data access that you must protect.

        A Offline
        A Offline
        andyP
        wrote on last edited by
        #3

        @SGaist: thank you for your help!

        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