Back to Blog home

Playing with coroutines and Qt

Published on Monday May 28, 2018 by Jesús Fernández in Dev Loop Qt courutines | Comments

Hello!
I was recently wondering about the status of the coroutines in C++ and I found several implementations. I decided to choose this one for my experiment.
It's simple and easy to use and works in Linux and Windows.

My goal was to try to find a way to have code running asynchronously without waiting for signals to trigger my slots and avoiding to call QCoreApplication::processEvents or creating a QEventLoop in the stack.

My first approach was to convert the processEvent function of a custom event dispatcher into a coroutine and use yield. After several failures, I decided not to continue this way.

My next attempt was to convert a slot into a coroutine:

QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([]() { ... });

Inside this lambda, the CPU will execute the code until the yield, it will jump back to the application event loop.
The full code is:

#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QPushButton fibonacciButton("0: 0");
fibonacciButton.show();
QObject::connect(&fibonacciButton, &QPushButton::pressed,
std::bind(&coroutine::resume, coroutine::create([&]() {
qulonglong f0 = 1, f1 = 0, n = 1;
fibonacciButton.setText(QString("1: 1"));
coroutine::yield();
fibonacciButton.setText(QString("2: 1"));
coroutine::yield();
forever {
auto next = f1 + f0;
f0 = f1;
f1 = next;
fibonacciButton.setText(QString("%0: %1").arg(n++).arg(f0 + f1));
coroutine::yield();
}
})));
return app.exec();
}

Here we can see a button connected to a lambda function which calculates the numbers in the Fibonacci sequence. After calculating the next number, we call yield and this will jump from this function to the event loop. When the user presses the button again, the code will return to the next line after the yield.

This example works because the user needs to press the button again to resume the execution of the code.

However, sometimes we want to resume the execution automatically. To do this, we need to yield the execution and schedule a resume of the execution:

void qYield()
{
const auto routine = coroutine::current();
QTimer::singleShot(0, std::bind(&coroutine::resume, routine));
coroutine::yield();
}

The first line gets the identifier of the coroutine and the second schedules the resume. With yield the CPU will come back to the previous frames and finally to the main loop with a resume enqueued resuming the code unconditionally.

Next step is to try to resume when a condition happens. Qt provides signals that indicate when something happened, so the more optimal way to yield the execution is:

template <typename Func>
void qAwait(const typename QtPrivate::FunctionPointer::Object *sender, Func signal)
{
const auto routine = coroutine::current();
const auto connection = QObject::connect(sender, signal,
std::bind(&coroutine::resume, routine));
coroutine::yield();
QObject::disconnect(connection);
}

Instead of enqueuing a resume we create a temporary connection to resume the execution of our slot.

An example of this can be:

#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QPlainTextEdit textEdit;
textEdit.show();
QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([&]() {
QUrl url("http://download.qt.io/online/qt5/linux/x64/online_repository/Updates.xml");
QNetworkRequest request(url);
QNetworkAccessManager manager;
auto reply = manager.get(request);
qAwait(reply, &QNetworkReply::finished);
textEdit.setPlainText(reply->readAll());
reply->deleteLater();
})));
return app.exec();
}

Here, I created a QTextEdit which receives the content of a file from internet. When the QNetworkReply finishes, the data is written in the QTextEdit.

Another example:

#include "coroutine.h"
#include <QtCore>
#include <QtWidgets>
#include <QtNetwork>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QPlainTextEdit edit;
edit.show();

QTimer::singleShot(0, std::bind(&coroutine::resume, coroutine::create([&]() {
auto previousText = edit.toPlainText();
forever {
if (edit.toPlainText() == QStringLiteral("quit")) {
qApp->quit();
} else if (previousText != edit.toPlainText()) {
qDebug() << previousText << "->" << edit.toPlainText();
previousText = edit.toPlainText();
}
qAwait(&edit, &QPlainTextEdit::textChanged);
qDebug() << "QPlainTextEdit::textChanged";
}
})));
return app.exec();
}

This application prints the text every time the user modifies the text, and it finishes the execution when the user writes the word 'quit.'

Subscribe to Our Blog

Stay up to date with the latest marketing, sales and service tips and news.

We are updating our comment system and you could face some issues. Please write to us at feedback@qt.io to report issues/bugs.