SkyFrotza
SkyFrotza

Reputation: 97

How to share variable from thread to many thread in QT

I have problem to share variable from thread1 to thread2 and main.cpp. My variable in thread1 has a name "value". I have shared to thread2 and make an increment in main.cpp. This is my code in thread1.h :

#ifndef THREAD1_H
#define THREAD1_H

#include <QCoreApplication>
#include <QDebug>

class thread1 : public QObject
{
    Q_OBJECT
public:
    thread1();
    void changeValue(int input);

    int value;
};

#endif // THREAD1_H

My code in thread1.cpp:

#include "thread1.h"

void thread1::changeValue(int input)
{
    if(input > 10 && input < 15)
        qDebug() << "hello world 1...";
    else if(input > 15 && input < 20)
        qDebug() << "Hello world 2...";
}

thread1::thread1()
{
}

My code in thread2.h:

#ifndef THREAD2_H
#define THREAD2_H

#include <QThread>
#include "thread1.h"

class thread2 : public QThread
{
    Q_OBJECT
public:
    thread2(QObject *parent = 0);
    void checkthread1();

private:
    thread1 *thr;
};

#endif // THREAD2_H

My code in thread2.cpp:

#include "thread2.h"

void thread2::checkthread1()
{
    thr = new thread1();
    qDebug() << "thread 1 value from thread2.c = " << QString::number(thr->value);
    thr->changeValue(thr->value);
}

thread2::thread2(QObject *parent) : QThread(parent)
{
}

My code in main.cpp:

#include <QCoreApplication>
#include <QTime>
#include <QDebug>
#include "thread1.h"
#include "thread2.h"

//Global Variable
thread2 *thr2;
thread1 *thr1;

void delay(int waited)
{
    QTime dieTime = QTime::currentTime().addMSecs(waited);
    while(QTime::currentTime() < dieTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    thr1 = new thread1();
    thr2 = new thread2();

    while(1)
    {
        thr1->value += 1;
        qDebug() << "thread 1 value from main.c = " << QString::number(thr1->value);
        thr2->checkthread1();
        delay(1000);
    }
    return a.exec();
}

And my result is:

thread 1 value from main.c =  "17"
thread 1 value from thread2.c =  "48"
thread 1 value from main.c =  "18"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "19"
thread 1 value from thread2.c =  "7143547"
thread 1 value from main.c =  "20"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "21"
thread 1 value from thread2.c =  "16"
Hello world 2...
thread 1 value from main.c =  "22"
thread 1 value from thread2.c =  "56"
thread 1 value from main.c =  "23"
thread 1 value from thread2.c =  "8"
thread 1 value from main.c =  "24"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "25"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "26"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "27"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "28"
thread 1 value from thread2.c =  "48"
thread 1 value from main.c =  "29"
thread 1 value from thread2.c =  "1818326560"
thread 1 value from main.c =  "30"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "31"
thread 1 value from thread2.c =  "16"
Hello world 2...
thread 1 value from main.c =  "32"
thread 1 value from thread2.c =  "0"
thread 1 value from main.c =  "33"
thread 1 value from thread2.c =  "0"

"value" in main.cpp make a good increment like what i want. I have desired "value" in thread2.cpp have same value like in main.cpp. But the result give me another value. I'm just little bit confused. Why "value" in thread2.cpp is change?

Upvotes: 0

Views: 994

Answers (2)

MSalters
MSalters

Reputation: 179779

You seem to still be struggling with the basics, such as keeping track of the objects you have. You claim to have two threads, but that's wrong: you keep creating new threads every time you call checkthread1().

You also fail to call delete, which you should only do after you know a thread has exited.

Since you have so many threads, you're not actually changing the same value. That's causing the problem you see. You'll need an atomic value once you fixed the thread spam

Upvotes: 1

recovery
recovery

Reputation: 3

QMutex and other similar, when you use qmutex you guarantee that only one thread at the moment have access to variable. You can see examples on qt documentation site. threads qt

Upvotes: 0

Related Questions