Introduction to Qt (2)

Taking the QtConcurrent::run() function in Qt as an example, this paper introduces how to run the function in a single thread.

1  QtConcurrent::run()

QtConcurrent is a namespace that provides a high-level interface function (APIs) to automatically adjust the number of threads running according to the actual number of CPU s in the current computer.

The following is the self-contained routine runfunction in Qt, corresponding to the installation directory D: Qt Qt5.8.0 Examples Qt-5.8 qtconcurrent runfuction runfuction

1.1. pro Engineering Document

Using the QtConcurrent module, you need to add: QT += concurrent in. pro

QT += concurrent widgets
CONFIG += console
CONFIG -= app_bundle

SOURCES += main.cpp

1.2  main.cpp

 1 #include <QApplication>
 2 #include <QDebug>
 3 #include <QThread>
 5 #include <QString>
 6 #include <QtConcurrent>
 8 void func(QString name)
 9 {
10     qDebug() << "Hello" << name << "from" << QThread::currentThread();
11 }
13 int main(int argc, char **argv)
14 {
15     QApplication app(argc, argv);
17     QFuture<void> fut1 = QtConcurrent::run(func, QString("Thread 1"));
18     QFuture<void> fut2 = QtConcurrent::run(func, QString("Thread 2"));
20     fut1.waitForFinished();
21     fut2.waitForFinished();
22 }

As you can see, using the QtConcurrent::run() function, func() is run in two different threads, and the output is as follows:

Hello "Thread 2" from QThread(0x3597f0, name = "Thread (pooled)")
Hello "Thread 1" from QThread(0x337720, name = "Thread (pooled)")


The following is a detailed explanation of the use of QtConcurrent::run(). After Reading 2 and 3, it's easy to understand the runfunction routine above.

2. General functions

2.1. Running a function in a thread

extern void func();
QFuture<void> future = QtConcurrent::run(func);

If you want to specify a thread pool for it, you can pass in the thread pool pointer as the first parameter

extern void func();
QThreadPool pool;
QFuture<void> future = QtConcurrent::run(&pool, func);

2.2 Transfer parameters to the function

The parameters to be passed need to be followed by the function name and added in turn

extern void FuncWithArguments(int arg1, const QString &string);

 int integer = ...;
 QString string = ...;

 QFuture<void> future = QtConcurrent::run(FuncWithArguments,integer,string);

2.3 Obtain the calculation results of this function

extern QString Func(const QByteArray &input);

QByteArray byte_array = ...;

QFuture<QString> future = QtConcurrent::run(func, byte_array);
QString result = future.result();

3. Membership functions

To run a member function in a class in a thread, a reference or pointer to an instance of that class can be passed in as the first parameter of QtConcurrent::run.

Constant member functions generally pass const references, while constant member functions generally pass pointers.

3.1 Constant member functions

In a separate thread, the constant member function split() of QByteArray is called, and the parameter passed to the run() function is bytearray.

  QByteArray bytearray = "hello world";
  QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ',');
  QList<QByteArray> result = future.result();

3.2 Non-constant member functions

In a separate thread, invertPixels(), a non-constant member function of QImage, is called, and the parameters passed to the run() function are & Image

  QImage image = ...;
  QFuture<void> future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba);
// At this point, the pixels in 'image' have been inverted


Reference material:

Qt Assistant | Qt 5.8 | Qt Concurrent | Concurrent Run

Keywords: C++ Qt Qt5

Added by Dan911 on Mon, 01 Jul 2019 21:14:37 +0300