如何在QT中从线程到多个线程共享变量

时间:2017-05-19 08:38:08

标签: c++ linux multithreading qt

我有问题从thread1到thread2和main.cpp共享变量。我在thread1中的变量有一个名称" value"。我已经与thread2共享并在main.cpp中进行了增量。这是我在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

我在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()
{
}

我在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

我在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)
{
}

我在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();
}

我的结果是:

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"

&#34;值&#34;在main.cpp中,像我想要的那样做一个很好的增量。我希望&#34;价值&#34;在thread2.cpp中具有与main.cpp中相同的值。但结果给了我另一个价值。我有点困惑。为什么&#34;价值&#34;在thread2.cpp中是改变吗?

2 个答案:

答案 0 :(得分:2)

您似乎仍在努力学习基础知识,例如跟踪您拥有的对象。您声称有两个线程,但这是错误的:每次调用checkthread1()时都会继续创建新线程。

您也无法调用delete,只有在您知道线程已退出后才应该这样做。

由于你有这么多线程,你实际上并没有改变相同的value。这导致你看到的问题。修复线程垃圾邮件后,您需要atomic

答案 1 :(得分:0)

QMutex和其他类似的,当你使用qmutex时,你保证目前只有一个线程可以访问变量。您可以在qt文档站点上查看示例。 threads qt