Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:28:00+00:00 2026-06-14T02:28:00+00:00

I have an application which runs 2 worker threads separate from the main GUI

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:28:02+00:00Added an answer on June 14, 2026 at 2:28 am

    It seems to me data is actually updated. But thread 1 is 10 times faster than thread 2. When you emit the Sleep signal, thread 2 is put to sleep for 100ms, which makes it unable to process other signals. Those will be placed in a queue and processed as soon as the control returns to the event loop. Then you’ll see the message with data updated.

    The specification anyway is quite weird for me: I read “thread 1 needs to send data to thread 2 every 100 ms….”, but I see you do it every 10ms, but then you say “thread 1 itself sleeps for 10ms in each loop of its run”. What is thread 1 supposed to do for the rest of the time?

    EDIT: I don’t think this is exactly what you wanted, but still I’m not completely sure I understand what you’re looking for. Not a complete or good implementation, just to give the idea:

    #include <QCoreApplication>
    #include <QTimer>
    #include <QThread>
    
    class Thread1 : public QThread
    {
       Q_OBJECT
    public:
       explicit Thread1() :
          data(0) {
          // Do nothing.
       }
    
       void run() {
          while (true) {
             data++;
             qDebug("Done some calculation here. Data is now %d.", data);
             emit dataChanged(data);
             usleep(10000);
          }
       }
    
    signals:
       void dataChanged(int data);
    
    private:
       int data;
    };
    
    class Thread2 : public QObject
    {
       Q_OBJECT
    public:
       explicit Thread2() {
          timer = new QTimer;
          connect(timer, SIGNAL(timeout()), this, SLOT(processData()));
          timer->start(100);
       }
    
       ~Thread2() {
          delete timer;
       }
    
    public slots:
       void dataChanged(int data) {
          this->data = data;
       }
    
       void processData() {
          qDebug("Processing data = %d.", data);
       }
    
    private:
       QTimer* timer;
       int data;
    };
    
    int main(int argc, char *argv[])
    {
       QCoreApplication a(argc, argv);
    
       Thread1 t1;
       Thread2 t2;
       qApp->connect(&t1, SIGNAL(dataChanged(int)), &t2, SLOT(dataChanged(int)));
       t1.start();
    
       return a.exec();
    }
    
    #include "main.moc"
    

    The output is:

    Done some calculation here. Data is now 1.
    Done some calculation here. Data is now 2.
    Done some calculation here. Data is now 3.
    Done some calculation here. Data is now 4.
    Done some calculation here. Data is now 5.
    Done some calculation here. Data is now 6.
    Done some calculation here. Data is now 7.
    Done some calculation here. Data is now 8.
    Done some calculation here. Data is now 9.
    Done some calculation here. Data is now 10.
    Processing data = 10.
    Done some calculation here. Data is now 11.
    Done some calculation here. Data is now 12.
    Done some calculation here. Data is now 13.
    Done some calculation here. Data is now 14.
    Done some calculation here. Data is now 15.
    Done some calculation here. Data is now 16.
    Done some calculation here. Data is now 17.
    Done some calculation here. Data is now 18.
    Done some calculation here. Data is now 19.
    Processing data = 19.
    Done some calculation here. Data is now 20.
    Done some calculation here. Data is now 21.
    Done some calculation here. Data is now 22.
    Done some calculation here. Data is now 23.
    Done some calculation here. Data is now 24.
    Done some calculation here. Data is now 25.
    Done some calculation here. Data is now 26.
    Done some calculation here. Data is now 27.
    Done some calculation here. Data is now 28.
    Processing data = 28.
    ...
    

    Beware that Thread2 actually is the main thread (i.e. UI thread) of your application. Move the object to a different thread if you need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which runs some multi-threading process. In each thread I'm sharing
Suppose I have a GUI application which has a background thread which runs for
I have an application which runs multiple threads. I use MadExcept to catch errors
I have a client application which runs as a Java applet from a user's
We have an application which runs in a JRE environment. The application uses some
In my java web application, I have a single background-worker thread, which requires a
I have an application which runs a tool that requires network connection. Now my
I have written a application which runs in the background. I want to write
I have an Stand alone Application which runs Shell Script(with parameters)in Ubuntu. ProcessBuilder pb1
I have written an ASP.NET MVC3 Web application which runs perfectly inside the Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.