Qt the simplest multithreading method QtConcurrent::run()

Qt the simplest multithreading method QtConcurrent::run() (Reprinted)

Recently, a software was written without considering the problem of multithreading. After the software is compiled, the mouse response will sometimes be delayed when the software performs calculation, but it is fully functional. Considering that the mouse response may be delayed more with the increase of calculation tasks, it is calculated to use the method of multithreading to throw the task of executing calculation to another thread for processing, It does not affect the response of the main interface to the mouse and various controls.

After checking the book and searching the Internet, the most introduced is to subclass QThread, and then overload run (). This operation can realize multithreading, but my software has basically taken shape. If it is realized by overloading run (), the software will change greatly. Then I will check the Internet to see if there is any simpler method to minimize the change of my software code, Finally, find the method that can use QtConcurrent::run(). According to the Internet, qtcurrent:: run() seems to be supported by the newer version of Qt, so it is not very common to search Qt multithreading operation on the Internet. According to the search results, I wrote a program for verification, and found that it is really very convenient, and the changes to the original software architecture are very small.

Create a new project and create ui interface.

First, it needs to be in the engineering document Add the following sentence to pro:

QT += concurrent

Add three buttons in the ui interface, as shown in the following figure:

The start button does not use multithreading to execute a dead loop (use this dead loop to replace the original function of the system). After clicking start, the whole software falls into a dead loop and cannot respond to any operation. Multithreading start is to execute the same dead loop through multithreading method, and the stop button is to exit the dead loop.

main.c code is as follows:

 1 #include "mainwindow.h"
 2 #include <QApplication>
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     MainWindow w;
 8     w.show();
10     return a.exec();
11 }

 mainwindow.h code is as follows:

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 4 #include <QMainWindow>
 6 namespace Ui {
 7 class MainWindow;
 8 }
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
14 public:
15     explicit MainWindow(QWidget *parent = 0);
16     void star();//Multi thread startup function, which is added to the original code
17     void xunhuan();//Dead loop function, use this function to replace the function of the original code
18     ~MainWindow();
20 private slots:
21     void on_pushButton_clicked();
22     void on_pushButton_2_clicked();
23     void on_pushButton_3_clicked();
25 private:
26     Ui::MainWindow *ui;
27     int myEnable;//Flag bit, used to exit the dead cycle operation
28 };
30 #endif // MAINWINDOW_H

mainwindow. The code of CPP is as follows:

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include<QDebug>
 4 #Include < qtconcurrent > / / remember to add the header file
 7 MainWindow::MainWindow(QWidget *parent) :
 8     QMainWindow(parent),
 9     ui(new Ui::MainWindow)
10 {
11     ui->setupUi(this);
12     myEnable = 0;//Dead cycle flag bit
14 }
17 void MainWindow::xunhuan()//Dead loop operation to replace the function of the original code
18 {
19    int i=0;
20     while(myEnable)
21     {
22         i++;
23         qDebug()<<i;
24     }
25 }
27 void MainWindow::star()//The startup function needs to be added on the basis of the original code
28 {
29     QtConcurrent::run(this,&MainWindow::xunhuan);//Multithreaded execution of dead cycle startup can take parameters, and the specific format can refer to other information on the Internet
30 }
33 MainWindow::~MainWindow()
34 {
35     delete ui;
36 }
38 void MainWindow::on_pushButton_clicked()
39 {
40     myEnable = 1;   
41     xunhuan(); //Non multithreaded execution loop
42 }
44 void MainWindow::on_pushButton_2_clicked()
45 {
46     myEnable=0;//Flag position zero, exit the dead cycle
47     qDebug()<<"Exit the loop!";
48 }
50 void MainWindow::on_pushButton_3_clicked()
51 {
53     myEnable = 1;
54     star();//Multithreaded start loop
56 }

The test results show that the non multithreading method is used to start the dead loop, the whole program falls into the dead loop, and the main interface cannot respond to any operation.

The program can also respond to the operation normally by using the multithreading method to start the dead cycle. After clicking the stop button, it can exit the dead cycle. The results are as follows:

Original link: https://www.cnblogs.com/ybqjymy/p/13446608.html

Tags: Qt

Posted by JellyFish on Sat, 21 May 2022 06:42:51 +0300