当前位置:网站首页>[QT] qtconcurrent:: run() + qtthreadpool to implement multithreading

[QT] qtconcurrent:: run() + qtthreadpool to implement multithreading

2020-11-10 14:03:25 Li Chungang

Past links :

stay QT In the development scenario , I think this method is also less used , Therefore, this article only makes a simple use of the introduction .QtConcurrent It's a namespace (namespace), It provides a high-level function interface (APIs), Make the program written , According to the computer CPU Check the number , Automatically adjust the number of running threads . This article takes Qt Medium QtConcurrent::run() Function as an example , Describes how to run functions in separate threads .

(1) Use  QtConcurrent modular , Need to be in .pro Add :

QT += concurrent

(2) Run a normal function on a separate thread :

#include <QApplication>
#include <QDebug>
#include <QThread>
#include <QtConcurrent>

void fun1(){
    qDebug()<<__FUNCTION__<<QThread::currentThread();
}

void fun2(QString str1, QString str2){
    qDebug()<<__FUNCTION__<<str1+str2<<QThread::currentThread();
}

int fun3(int i, int j){
    qDebug()<<__FUNCTION__<<QThread::currentThread();
    return i+j;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qDebug()<<__FUNCTION__<<QThread::currentThread();

    // Ordinary functions without parameters 
    QFuture<void> fut1 = QtConcurrent::run(fun1);

    // Ordinary functions with parameters 
    QFuture<void> fut2 = QtConcurrent::run(fun2, QString("Thread"),QString(" 2"));

    // Get the return value of a normal function 
    int i=1, j=2;
    QFuture<int> fut3 = QtConcurrent::run(fun3, i, j);
    qDebug()<<"ret:"<<fut3.result();

    // The above example , If you want to specify a thread pool for it , You can pass the pointer to the thread pool as the first parameter 
    QThreadPool pool;
    QFuture<void> fut4 = QtConcurrent::run(&pool, fun1);

    fut1.waitForFinished();
    fut2.waitForFinished();
    fut3.waitForFinished();
    fut4.waitForFinished();

    return a.exec();
}

Output results :

qMain QThread(0xf380590)
fun2 "Thread 2" QThread(0x1ca7c758, name = "Thread (pooled)")
fun1 QThread(0x1ca7c6d8, name = "Thread (pooled)")
fun3 QThread(0x1ca7c5b8, name = "Thread (pooled)")
ret: 3
fun1 QThread(0x1ca7c438, name = "Thread (pooled)")

(3) Run the member functions of the class in the thread separately :

Run the member function of a class in a thread , You can use a reference or pointer to an instance of this class as  QtConcurrent::run  The first parameter of is passed in , Constant member functions generally pass constant references  (const reference), Non constant member functions generally pass pointers  (pointer).

  • Constant member functions

In a separate thread , call  QByteArray  Constant member function of  split(), Pass to run() The argument to the function is  bytearray

// Constant member functions QByteArray::split()
QByteArray bytearray = "hello,world";
QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ',');
QList<QByteArray> result = future.result();
qDebug()<<"result:"<<result;
  • Non constant member function

In a separate thread , call  QImage  Non constant member function of  invertPixels(), Pass to run() The argument to the function is  &image

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

版权声明
本文为[Li Chungang]所创,转载请带上原文链接,感谢