gpt4 book ai didi

multithreading - 多线程应用

转载 作者:行者123 更新时间:2023-12-03 13:05:13 24 4
gpt4 key购买 nike

我有一个应用程序,它与主GUI线程分开运行2个工作线程。

线程1:

  • 需要每100毫秒将一些数据发送到线程2。
  • 在其运行的每个循环中 sleep 10毫秒。

  • 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;
    };

    执行:
    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);

    }
    }

    线程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;
    };

    执行:
    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(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();
    }

    输出:
    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

    线程2中的数据未使用线程1的最新值更新,并且GUI窗口被完全冻结。请让我知道是否有更好/更有效的方法来使用Qt实现多线程应用程序并在线程之间进行通信。

    编辑:根据LUCA ,Thread1几乎保持不变...而Thread2.h看起来像这样

    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;

    };

    和实现是... 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);
    }

    mainwindow.cpp 看起来像这样...
    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比线程2快10倍。当您发出“ sleep ”信号时,线程2进入休眠状态100ms,这使其无法处理其他信号。控件返回事件循环后,它们将被放入队列并进行处理。然后,您将看到消息,其中数据已更新。

    无论如何,该规范对我来说很奇怪:我读到“线程1需要每100毫秒向线程2发送数据....”,但我看到您每10毫秒发送一次,但是您说“线程1本身 sleep 10毫秒在其运行的每个循环中”。线程1在其余时间内应该做什么?

    编辑:我不认为这正是您想要的,但是我仍然不确定我是否完全了解您的需求。并不是一个完整的或好的实现,只是给出一个想法:

    #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"

    输出为:
    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.
    ...

    注意Thread2实际上是应用程序的主线程(即UI线程)。如果需要,将对象移动到其他线程。

    关于multithreading - 多线程应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330006/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com