Jesús Fernández

Playing with coroutines and Qt

Published Tuesday May 29th, 2018
8 Comments on Playing with coroutines and Qt
Posted in Dev Loop, Qt | Tags: ,

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.’

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Dev Loop, Qt | Tags: ,

8 comments

Qize Huang says:

I am implementing a QtNetworkNg using coroutine. I add a similar qAwait() to it after I saw this post.
#include "../../qtnetworkng.h"
using namespace qtng;
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QSharedPointer text(new QPlainTextEdit);
text->show();
CoroutineGroup operations;
operations.spawn([text] {
QNetworkAccessManager manager;
QUrl url("http://download.qt.io/online/qt5/linux/x64/online_repository/Updates.xml");
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
qAwait(reply, &QNetworkReply::finished);
text->setPlainText(reply->readAll());
reply->deleteLater();
});
return startQtLoop();
}

QtNetworkNg has more powerful API to operate coroutine, such as Event, Semaphore and RLock. And consider many corner cases for Qt GUI application. For example, Qt GUI application can not call `QMessageBox::information()` in coroutine, because it runs a local loop. Use `callInEventLoop({})` can address this problem.

More detail please visit the github page of QtNetworkNg.

Jesús Fernández Jesús Fernández says:

To make it compatible with QMessageBox. IMO it’s better to reimplement it completely to use coroutines instead of a nested event loop.

Louis says:

Hi,

Thanks for the article!

I have played a while writing my own Promise system using signals and slots (not events and certainly never processEvents() because this one leads to recursion bugs) to “wake” the callbacks (the syntax is a bit similar to https://github.com/bwalter/qt-promise but synchronous on the GUI thread). That makes me wonder about the following:

1) each promise has a lifetime (for example it will follow the lifetime of an object and when this object will be destroyed it has to destroy the promise as well): for coroutines, what if the signal is never called? Will it cause a memory leak?
2) promises have the concept of failure and success, but what about exception management in coroutines? With yelds, I believe it can quickly become complicated
3) how would you compare coroutines with promises (even synchronous ones)? For example on the subject of chaining operations

Regards,
Louis

Jesús Fernández Jesús Fernández says:

Hi Louis!
Thank you for the message and link to the project! I’ll check it 🙂

1) You need to destroy your coroutines. for simplicity I did not worry about the memory leaks for the examples, but yes you need to destroy the coroutine code finishes, and the slot is disconnected.

2) You need to be careful to select your signal. QNetworkReply::finished will be raised in case of error, so your code needs to check the error code before reading.

3) IMO using promises (and futures) is a different approach more based on functional programming. Coroutines are closer to old-school programming where you wait until something finishes to continue. They are handy to write code that blocks the UI if you don’t use them (or qApp->processEvents()).

Daniel Nicoletti says:

They are also handy to avoid nesting event loops which is why I am now researching on this, I had a stack overflow due nesting too many processEvents calls (my custom event loop doesn’t detect nesting)…

As far as I know this is proposed to C++20 right? Do you know if Qt will eventually provide support for that?

Best,

Jesús Fernández Jesús Fernández says:

Yes, my primary interest was to avoid nested event loops. I forgot to say this in the text.

> As far as I know this is proposed to C++20 right? Do you know if Qt will eventually provide support for that?

I wanted to try before updating my compiler and if possible to have something working with current versions of Qt and compilers. At some point, I will give a try to a newer version of clang to try to use their coroutine support.

TKM says:

Ah, so eventually fibers might get support for in C++ after almost 30 years ? 😉

There are quite few implementations of fiber-schedulers,
https://swtch.com/libtask/

And i would recommend
https://www.gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine

Jesús Fernández Jesús Fernández says:

Thank you!

Commenting closed.

Get started today with Qt Download now