I believe signal emission is always thread-safe.
class CalculationThread
: public QThread {
Q_OBJECT
public:
CalculationThread
(QObject *parent
= 0);
~CalculationThread();
void calculation(CalculationData & data);
void run();
signals:
void dataChanged();
void error
(const QString &message
);
private:
...
CalculationData m_data
...
};
void CalculationThread::calculation(CalculationData & data)
{
m_data = data;
if (!isRunning())
start();
...
}
void CalculationThread::run()
{
mutex.lock();
...
//do time consuming calculations
...
if(/*data changed*/)
emit dataChanged();
mutex.unlock();
}
{
...
private slots:
dataChangedCalc()
}
Plot::dataChangedCalc()
{
...
// create and attach QwtPlotItems to QwtPlots
...
this->replot()
}
connect(&thread, SIGNAL(dataChanged()), this, SLOT(dataChangedCalc()));
class CalculationThread : public QThread
{
Q_OBJECT
public:
CalculationThread(QObject *parent = 0);
~CalculationThread();
void calculation(CalculationData & data);
void run();
signals:
void dataChanged();
void error(const QString &message);
private:
...
CalculationData m_data
QMutex mutex;
...
};
void CalculationThread::calculation(CalculationData & data)
{
QMutexLocker locker(&mutex);
m_data = data;
if (!isRunning())
start();
...
}
void CalculationThread::run()
{
mutex.lock();
...
//do time consuming calculations
...
if(/*data changed*/)
emit dataChanged();
mutex.unlock();
}
class Plot: public QwtPlot
{
...
private slots:
dataChangedCalc()
}
Plot::dataChangedCalc()
{
...
// create and attach QwtPlotItems to QwtPlots
...
this->replot()
}
connect(&thread, SIGNAL(dataChanged()), this, SLOT(dataChangedCalc()));
To copy to clipboard, switch view to plain text mode
Note: the this pointer is a pointer to a Plot
Is the problem that I create and attach QwtPlotItems to QwtPlots within a worker thread?
I assuming that you are creating and attaching many plotItems (that's your motivation for putting this activity in the worker thread)
QObject and subclasses thereof are not thread safe. QwtPlot is asubclas of QObject.
Bookmarks