Hi,

I need to have access to several information from different threads so I made a class like this:
Qt Code:
  1. class Foo
  2. {
  3. public:
  4. void increase()
  5. {
  6. QWriteLocker l(&m_lock);
  7. m_ints << 42;
  8. }
  9.  
  10. private:
  11. QVector<int> m_ints;
  12. };
To copy to clipboard, switch view to plain text mode 
Foo is then instantiated in the main thread and a pointer is passed to the relevant threads. So far so good. Now I'd like to get informed if e.g. m_ints gets changed. So QObject came into play but as it is only reentrant (except connect and disconnect) it is getting complicated.

Let's think of this modified class:
Qt Code:
  1. class Foo : public QObject
  2. {
  3. public:
  4. Foo: QObject(0) {}
  5.  
  6. void increase()
  7. {
  8. QWriteLocker l(&m_lock);
  9. m_ints << 42;
  10. emit added(42);
  11. }
  12.  
  13. signals:
  14. void added(int);
  15.  
  16. private:
  17. QVector<int> m_ints;
  18. };
To copy to clipboard, switch view to plain text mode 
So, Foo's thread affinity is my main thread.
  1. Is this class still thread-safe since it does not use any functions of QObject? What about emit?
  2. What does this mean for calling increase() through a pointer from a different tread. Which thread does the "work" (manipulating m_ints) of increase(): The main thread or the local thread?


If that solution is bad, what would be a good approach to get informed about changes? (Beside the caller of the function emits afterwards the changes ala
Qt Code:
  1. myFoo->increase();
  2. emit mySignalAdded(42);
To copy to clipboard, switch view to plain text mode 


Thanks
Lykurg