Remember that slots are always called by that thread where the slot's object lives.
So, in your case you have probably created the WorkerThread in the main thread and then you have started it with start()
// executing in main thread
worker = new WorkerThread; // created in main thread so it lives in it too
worker->start();
// executing in main thread
worker = new WorkerThread; // created in main thread so it lives in it too
worker->start();
To copy to clipboard, switch view to plain text mode
Now, when timeout() signal is emitted in the worker thread the main thread would call the update() slot if the execution would be inside the event loop. But, since the execution is currently blocked inside the wait() call, nothing happens.
To fix this, the WorkerThread object would need to live in the worker thread.
I'll modify your class a bit:
{
Q_OBJECT
public:
~Worker();
private slots:
void run();
void update();
private:
volatile bool m_isInterrupted;
};
class Worker : public QObject
{
Q_OBJECT
public:
~Worker();
private slots:
void run();
void update();
private:
volatile bool m_isInterrupted;
};
To copy to clipboard, switch view to plain text mode
Then in main thread where you created the worker:
// executing in main thread
worker = new Worker; // created in main thread so it lives in it too
worker->moveToThread( thread ); // change thread where the worker lives
thread->start();
QTimer::singleShot( 0, worker,
SLOT(run
()) );
// ask 'thread' to call the run() slot
// executing in main thread
thread = new QThread;
worker = new Worker; // created in main thread so it lives in it too
worker->moveToThread( thread ); // change thread where the worker lives
thread->start();
QTimer::singleShot( 0, worker, SLOT(run()) ); // ask 'thread' to call the run() slot
To copy to clipboard, switch view to plain text mode
Bookmarks