Helper classes for coroutines in Qt.
- Create and run coroutines from Qt in a platform independent manner
- Supports Windows and unix
- Manage coroutines with yield, resume, cancel and abort
- Generic await method to create custom awaitables
- Predefined awaitables
timeout
: awaits an internal QTimer that triggers after a specified timesignal
: awaits the emission of a signal, can return the signal argumentsiodevice
: awaits the readready signal of QIODevice and returns the read datafuture
: awaits the completition of a QFuture
- Async iterator class with
yield_return
method for easy iteration - Async Queue class to enqueue to a coroutine that waits for data
The package is providet as qpm package, de.skycoder42.qtcoroutines
. You can install it either via qpmx (preferred) or directly via qpm.
qpmx is a frontend for qpm (and other tools) with additional features, and is the preferred way to install packages. To use it:
- Install qpmx (See GitHub - Installation)
- Install qpm (See GitHub - Installing)
- In your projects root directory, run
qpmx install de.skycoder42.qtcoroutines
- Install qpm (See GitHub - Installing)
- In your projects root directory, run
qpm install de.skycoder42.qtcoroutines
- Include qpm to your project by adding
include(vendor/vendor.pri)
to your.pro
file
Check their GitHub - Usage for App Developers to learn more about qpm.
The general usage is: You create a coroutine from a function and then execute it. Inside the coroutine you can yield
(or await
) to return execution back to the caller until the method is resumed (either explicitly or because of an
await condition beeing triggered):
void coroutine() {
qDebug() << "Hello from coroutine";
QtCoRoutine::yield();
qDebug() << "Goodbye from coroutine";
}
void coroutine2() {
qDebug() << "I will continue in 3 seconds";
QtCoroutine::await(std::chrono::seconds(3));
qDebug() << "Time to quit!";
qApp->quit();
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
auto routine = QtCoroutine::create(coroutine);
qDebug() << "Hello from main";
QtCoroutine::resume(routine);
qDebug() << "Goodbye from main";
QtCoroutine::resume(routine);
QtCoroutine::createAndRun(coroutine2);
qDebug() << "Lets run the app...";
return app.exec();
}
The code above will produce the following output:
Hello from main
Hello from coroutine
Goodbye from main
Goodbye from coroutine
I will continue in 3 seconds
Lets run the app...
Time to quit!