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:
#ifndef DATATABLE_H
#define DATATABLE_H
#include <QObject>
#include <QReadWriteLock>
{
Q_OBJECT
public:
explicit DataTable
(QObject *parent
= 0);
void setIntegerData(int);
int getIntegerData();
private:
int integerdata;
};
#endif // DATATABLE_H
#ifndef DATATABLE_H
#define DATATABLE_H
#include <QObject>
#include <QReadWriteLock>
class DataTable : public QObject
{
Q_OBJECT
public:
explicit DataTable(QObject *parent = 0);
void setIntegerData(int);
int getIntegerData();
private:
int integerdata;
QReadWriteLock lock;
};
#endif // DATATABLE_H
To copy to clipboard, switch view to plain text mode
-implementation:
#include "datatable.h"
DataTable
::DataTable(QObject *parent
) :{
}
void DataTable::setIntegerData(int i)
{
integerdata=i;
}
int DataTable::getIntegerData()
{
return integerdata;
}
#include "datatable.h"
DataTable::DataTable(QObject *parent) :
QObject(parent)
{
}
void DataTable::setIntegerData(int i)
{
QWriteLocker wlocker(&lock);
integerdata=i;
}
int DataTable::getIntegerData()
{
QReadLocker rlocker(&lock);
return integerdata;
}
To copy to clipboard, switch view to plain text mode
Bookmarks