BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Qt5 Tutorial Creating QThread using QtConcurrent - 2020





Bookmark and Share





bogotobogo.com site search:




Creating Threads

In this tutorial, we will learn how to create Threads using QtConcurrent Framework.


In the previous example of creating thread from QThread is not a recommended way of using thread in Qt as we can see from the Qt5 document below:

The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives such as mutexes, read-write locks, wait conditions, or semaphores.

Programs written with QtConcurrent automatically adjust the number of threads used according to the number of processor cores available. This means that applications written today will continue to scale when deployed on multi-core systems in the future.




Creating a Thread - Modifying the code from the previous example

In this section, we'll get the same output as in the previous the previous example, QThreads - Creating Threads with much more short and concise way.

Here is the code:

#include <QCoreApplication>
#include <qtconcurrentrun.h>
#include <QThread>

#ifndef QT_NO_CONCURRENT

void myRunFunction(QString name)
{
    for(int i = 0; i <= 5; i++)
    {
        qDebug() << name << " " << i <<
                    "from" << QThread::currentThread();
    }
}

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

    QFuture<void> t1 = QtConcurrent::run(myRunFunction, QString("A"));
    QFuture<void> t2 = QtConcurrent::run(myRunFunction, QString("B"));
    QFuture<void> t3 = QtConcurrent::run(myRunFunction, QString("C"));

    t1.waitForFinished();
    t2.waitForFinished();
    t3.waitForFinished();

    return a.exec();
}

#else

#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QString text("Qt Concurrent is not yet supported on this platform");

    QLabel *label = new QLabel(text);
    label->setWordWrap(true);

    label->show();
    qDebug() << text;

    app.exec();
}
#endif

Also, we need to add a line to .pro file like this:

QT       += concurrent

Output:

"A"   0 from QThread(0xaff287a000, name = "Thread (pooled)")
"B"   0 from QThread(0xaff287dd30, name = "Thread (pooled)")
"A"   1 from QThread(0xaff287a000, name = "Thread (pooled)")
"A"   2 from QThread(0xaff287a000, name = "Thread (pooled)")
"B"   1 from QThread(0xaff287dd30, name = "Thread (pooled)")
"A"   3 from QThread(0xaff287a000, name = "Thread (pooled)")
"B"   2 from QThread(0xaff287dd30, name = "Thread (pooled)")
"A"   4 from QThread(0xaff287a000, name = "Thread (pooled)")
"B"   3 from QThread(0xaff287dd30, name = "Thread (pooled)")
"A"   5 from QThread(0xaff287a000, name = "Thread (pooled)")
"B"   4 from QThread(0xaff287dd30, name = "Thread (pooled)")
"B"   5 from QThread(0xaff287dd30, name = "Thread (pooled)")
"C"   0 from QThread(0xaff287a000, name = "Thread (pooled)")
"C"   1 from QThread(0xaff287a000, name = "Thread (pooled)")
"C"   2 from QThread(0xaff287a000, name = "Thread (pooled)")
"C"   3 from QThread(0xaff287a000, name = "Thread (pooled)")
"C"   4 from QThread(0xaff287a000, name = "Thread (pooled)")
"C"   5 from QThread(0xaff287a000, name = "Thread (pooled)")

As we can see, we do not have to create a thread by ourselves. All we have to do is to define a run function with the proper parameter feeds. QtConcurrent handles all the details for us.

In this example, we used couple of APIs from QtConcurrent:

  1. QtConcurrent::run() - runs a function in another thread.
  2. QFuture - represents the result of an asynchronous computation.
    Actually, QFuture also offers ways to interact with a runnning computation such as cancel(), pause(), or resume(). However, we cannot do that in our example because the future was returned by QtConcurrent::run(). We'll deal with this interaction with the result later section for QtConcurrent::mappedReduced().











Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Qt 5 Tutorial



Hello World

Signals and Slots

Q_OBJECT Macro

MainWindow and Action

MainWindow and ImageViewer using Designer A

MainWindow and ImageViewer using Designer B

Layouts

Layouts without Designer

Grid Layouts

Splitter

QDir

QFile (Basic)

Resource Files (.qrc)

QComboBox

QListWidget

QTreeWidget

QAction and Icon Resources

QStatusBar

QMessageBox

QTimer

QList

QListIterator

QMutableListIterator

QLinkedList

QMap

QHash

QStringList

QTextStream

QMimeType and QMimeDatabase

QFile (Serialization I)

QFile (Serialization II - Class)

Tool Tips in HTML Style and with Resource Images

QPainter

QBrush and QRect

QPainterPath and QPolygon

QPen and Cap Style

QBrush and QGradient

QPainter and Transformations

QGraphicsView and QGraphicsScene

Customizing Items by inheriting QGraphicsItem

QGraphicsView Animation

FFmpeg Converter using QProcess

QProgress Dialog - Modal and Modeless

QVariant and QMetaType

QtXML - Writing to a file

QtXML - QtXML DOM Reading

QThreads - Introduction

QThreads - Creating Threads

Creating QThreads using QtConcurrent

QThreads - Priority

QThreads - QMutex

QThreads - GuiThread

QtConcurrent QProgressDialog with QFutureWatcher

QSemaphores - Producer and Consumer

QThreads - wait()

MVC - ModelView with QListView and QStringListModel

MVC - ModelView with QTreeView and QDirModel

MVC - ModelView with QTreeView and QFileSystemModel

MVC - ModelView with QTableView and QItemDelegate

QHttp - Downloading Files

QNetworkAccessManager and QNetworkRequest - Downloading Files

Qt's Network Download Example - Reconstructed

QNetworkAccessManager - Downloading Files with UI and QProgressDialog

QUdpSocket

QTcpSocket

QTcpSocket with Signals and Slots

QTcpServer - Client and Server

QTcpServer - Loopback Dialog

QTcpServer - Client and Server using MultiThreading

QTcpServer - Client and Server using QThreadPool

Asynchronous QTcpServer - Client and Server using QThreadPool

Qt Quick2 QML Animation - A

Qt Quick2 QML Animation - B

Short note on Ubuntu Install

OpenGL with QT5

Qt5 Webkit : Web Browser with QtCreator using QWebView Part A

Qt5 Webkit : Web Browser with QtCreator using QWebView Part B

Video Player with HTML5 QWebView and FFmpeg Converter

Qt5 Add-in and Visual Studio 2012

Qt5.3 Installation on Ubuntu 14.04

Qt5.5 Installation on Ubuntu 14.04

Short note on deploying to Windows




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong













Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master