I have an application which runs 2 worker threads separate from the main GUI thread.
Thread 1:
- needs to send some data to thread 2 every 100 ms.
- sleeps for 10ms in each loop of its run.
Header:
class thread1:public QThread
{
Q_OBJECT
public:
thread1();
~thread1();
signals:
void wakeThread2();
void sendValue(int);
void sleepThread2();
protected:
void run();
private:
volatile bool stop;
int data;
};
Implementation:
thread1::thread1():stop(false),data(0)
{
}
void thread1::run()
{
while(!stop)
{
++data;
if(data==1000)
data = 0;
cout<<"IN THREAD 1 with data = "<<data<<endl;
emit sendValue(data);
emit wakeThread2();
emit sleepThread2();
msleep(10);
}
}
Thread 2
Header:
class thread2:public QThread
{
Q_OBJECT
public:
thread2();
~thread2();
private slots:
void receiveValue(int);
void Sleep();
protected:
void run();
private:
volatile bool stop;
int data;
};
Implementation:
thread2::thread2():stop(false),data(0)
{
}
void thread2::run()
{
if(!stop)
cout<<"IN THREAD..............2 with data = "<<data<<endl;
}
void thread2::receiveValue(int x)
{
data = x;
}
void thread2::Sleep()
{
msleep(100);
}
MainWindow:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
t1 = new thread1;
t2 = new thread2;
QObject::connect(t1,SIGNAL(wakeThread2()),t2,SLOT(start()));
QObject::connect(t1,SIGNAL(sendValue(int)),t2,SLOT(receiveValue(int)));
QObject::connect(t1,SIGNAL(sleepThread2()),t2,SLOT(Sleep()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_startT1_clicked()
{
t1->start();
}
Output:
IN THREAD 1 with data = 1
IN THREAD..............2 with data = 1
IN THREAD 1 with data = 2
IN THREAD 1 with data = 3
IN THREAD 1 with data = 4
IN THREAD 1 with data = 5
IN THREAD 1 with data = 6
IN THREAD 1 with data = 7
IN THREAD 1 with data = 8
IN THREAD 1 with data = 9
IN THREAD 1 with data = 10
IN THREAD 1 with data = 11
IN THREAD..............2 with data = 2
IN THREAD 1 with data = 12
IN THREAD 1 with data = 13
IN THREAD 1 with data = 14
IN THREAD 1 with data = 15
IN THREAD 1 with data = 16
IN THREAD 1 with data = 17
IN THREAD 1 with data = 18
IN THREAD 1 with data = 19
IN THREAD 1 with data = 20
The data in thread 2 is not getting updated with the latest value of thread 1 and the GUI window is totally frozen. Please let me know if there is better/more efficient way to implement multi thread applications with Qt and to communicate between threads.
EDIT : ACCORDING TO LUCA the Thread1 remains almost the same...while Thread2.h looks like this
Thread2.h
#include <QThread>
#include <QTimer>
#include "iostream"
using namespace std;
class Thread2 : public QThread
{
Q_OBJECT
public:
Thread2();
~Thread2();
void startThread();
public slots:
void receiveData(int);
protected:
void run();
private:
volatile bool stop;
int data;
QTimer *timer;
};
and Implementation is....Thread2.cpp..
#include "thread2.h"
Thread2::Thread2():stop(false),data(0)
{
timer = new QTimer;
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(start()));
}
Thread2::~Thread2()
{
delete timer;
}
void Thread2::receiveData(int x)
{
this->data = x;
}
void Thread2::run()
{
cout<<"thread 2 .........data = "<<data<<endl;
}
void Thread2::startThread()
{
timer->start(100);
}
and the mainwindow.cpp looks like this...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
t1 = new Thread1;
t2 = new Thread2;
QObject::connect(t1,SIGNAL(sendData(int)),t2,SLOT(receiveData(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_start_thread1_clicked()
{
t1->start();
t2->startThread();
}