Http://www.cnblogs.com/xinxue/p/6840315.html
The qtconcurrent of Qt
This article takes the Qtconcurrent::run () function in Qt as an example to show how to run a function in a separate thread.
1 Qtconcurrent::run ()
Qtconcurrent is a namespace that provides a high-level function interface (APIS) that allows the program to automatically adjust the number of threads that are running based on the actual number of CPU cores in the current computer.
The following is the self-contained routine in Qt runfunction, which corresponds to the installation directory D:\Qt\Qt5.8.0\Examples\Qt-5.8\qtconcurrent\runfucntion
1.1. Pro Project Files
Using the Qtconcurrent module, you need to add it in. Pro: QT + = Concurrent
QT + = Concurrent Widgetsconfig + = consoleconfig-app_bundlesources + = Main.cpp
1.2 main.cpp
1 #include <QApplication> 2 #include <QDebug> 3 #include <QThread> 4 5 #include <QString> 6 #i Nclude <QtConcurrent> 7 8 void func (QString name) 9 { qdebug () << "Hello" << name << "fr Om "<< qthread::currentthread ();}12 int main (int argc, char **argv) ( qapplication, argc), 16 qfuture<void> fut1 = Qtconcurrent::run (func, QString ("Thread 1")); qfuture<void> fut2 = Qtconcurrent::run (func, QString ("Thread 2"); fut1.waitforfinished (); fut2.waitforfinished (); 22}
As you can see, using the Qtconcurrent::run () function, the Func () is run in two different threads, with the result of the output as follows:
Hello "Thread 2" from Qthread (0x3597f0, name = "Thread (Pooled)") Hello "Thread 1" from Qthread (0x337720, name = "Thread" (p ooled) ")
Here's a detailed explanation of using Qtconcurrent::run (), reading 2 and 3, and then looking at the runfunction routines above, it's very easy to understand.
2 Common functions
2.1 To run 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 a pointer to the thread pool as the first argument in
extern void Func (); Qthreadpool Pool; qfuture<void> future = Qtconcurrent::run (&pool, func);
2.2 Passing parameters to the function
Parameters that need to be passed, followed by the function name, followed by
extern void funcwitharguments (int arg1, const QString &string); int integer = ...; QString string = ...; qfuture<void> future = Qtconcurrent::run (funcwitharguments,integer,string);
2.3 Get the calculation result of the function
extern QString Func (const qbytearray &input); Qbytearray Byte_array = ...; qfuture<qstring> future = Qtconcurrent::run (func, Byte_array); QString result = Future.result ();
3 member functions
To run a member function in a class in a thread, you can pass a reference or pointer to the instance of the class as the first argument to the Qtconcurrent::run.
A constant member function generally passes a constant reference (const reference), whereas a generic member function generally passes a pointer (pointer)
3.1 Constant member functions
In a separate thread, call Qbytearray's constant member function split (), and the argument 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 A very-mass member function
In a separate thread, the Qimage member function called Invertpixels (), which is passed to the run () function, is &image
Qimage image = ...; qfuture<void> future = Qtconcurrent::run (&image, &qimage::invertpixels, Qimage::invertrgba); ... Future.waitforfinished ();
At this point, the pixels in ' image ' has been inverted
QT Create thread