Hi, I need to share some variables between several treads and I'm not really sure about how to do it.
Right now i think i can create a class to store the values and write the setters and getters with the lock mechanisms to protect the variables. Then i can pass a reference to the container object to all the involved threads.
Would this be an effective way to share?
The storage class would be like this:
-header:
Qt Code:
  1. #ifndef DATATABLE_H
  2. #define DATATABLE_H
  3.  
  4. #include <QObject>
  5. #include <QReadWriteLock>
  6.  
  7. class DataTable : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit DataTable(QObject *parent = 0);
  12. void setIntegerData(int);
  13. int getIntegerData();
  14.  
  15. private:
  16. int integerdata;
  17. };
  18.  
  19. #endif // DATATABLE_H
To copy to clipboard, switch view to plain text mode 

-implementation:
Qt Code:
  1. #include "datatable.h"
  2.  
  3.  
  4. DataTable::DataTable(QObject *parent) :
  5. QObject(parent)
  6. {
  7. }
  8.  
  9. void DataTable::setIntegerData(int i)
  10. {
  11. QWriteLocker wlocker(&lock);
  12. integerdata=i;
  13. }
  14.  
  15. int DataTable::getIntegerData()
  16. {
  17. QReadLocker rlocker(&lock);
  18. return integerdata;
  19. }
To copy to clipboard, switch view to plain text mode