QFuture is usually used together with QtConcurrent to represent the result of an asynchronous computation. It is a powerful component for multi-thread programming. But its usage is limited to the result of threads. And QtConcurrent only provides MapReduce / FilterReduce model that may not fit your use cases. Moreover, it doesn't work with the asynchronous signal emitted by QObject. And it is a bit trouble to setup the listener function via QFutureWatcher.
AsyncFuture is designed to enhance the function to offer a better way to use it for asynchronous programming. It provides a Promise object like interface. This project is inspired by AsynQt and RxCpp.
Remarks: You may use this project together with QuickFuture for QML programming.
1. Convert a signal from QObject into a QFuture object
#include "asyncfuture.h"
using namespace AsyncFuture;
// Convert a signal from QObject into a QFuture object
QFuture<void> future = observe(timer, &QTimer::timeout).future();
/* Listen from the future without using QFutureWatcher<T>*/
observe(future).subscribe([]() {
// onCompleted. It is invoked when the observed future is finished successfully
qDebug() << "onCompleted";
},[]() {
// onCanceled
qDebug() << "onCancel";
});
/* It is chainable. Listen from a timeout signal only once */
observe(timer, &QTimer::timeout).subscribe([=]() { /*…*/ });
2. Combine multiple futures with different type into a single future object
/* Combine multiple futures with different type into a single future */
QFuture<QImage> f1 = QtConcurrent::run(readImage, QString("image.jpg"));
QFuture<void> f2 = observe(timer, &QTimer::timeout).future();
QFuture<QImage> result = (combine() << f1 << f2).subscribe([=](){
// Read an image but do not return before timeout
return f1.result();
}).future();
3. Use QFuture like a Promise object
Create a QFuture then complete / cancel it by yourself.
// Complete / cancel a future on your own choice
auto d = deferred<bool>();
d.subscribe([]() {
qDebug() << "onCompleted";
}, []() {
qDebug() << "onCancel";
});
d.complete(true); // or d.cancel();
QCOMPARE(d.future().isFinished(), true);
QCOMPARE(d.future().isCanceled(), false);
Complete / cancel a future according to another future object.
// Complete / cancel a future according to another future object.
auto d = deferred<void>();
d.complete(QtConcurrent::run(timeout));
QCOMPARE(d.future().isFinished(), false);
QCOMPARE(d.future().isCanceled(), false);
Read a file. If timeout, cancel it.
auto timeout = observe(timer, &QTimer::timeout).future();
auto defer = deferred<QString>();
defer.complete(QtConcurrent::run(readFileworker, fileName));
defer.cancel(timeout);
return defer.future();
4. Chainable Future - Advanced multi-threading programming model
Futures can be chained into a sequence of process. And represented by a single future object.
/* Start a thread and process its result in main thread */
QFuture<QImage> readImage(const QString& file) {
auto readImageWorker = [=]() {
QImage image;
image.read(file);
return image;
};
auto updateCache = [&](QImage image) {
m_cache[file] = image;
return image;
};
QFuture<QImage> reading = QtConcurrent::run(readImageWorker));
return observe(reading).subscribe(updateCache).future();
}
// Read image by a thread, when it is ready, run the updateCache function
// in the main thread.
// And it return another QFuture to represent the final result.
/* Start a thread and process its result in main thread, then start another thread. */
QFuture<int> f1 = QtConcurrent::mapped(input, mapFunc);
QFuture<int> f2 = observe(f1).subscribe([=](QFuture<int> future) {
// You may use QFuture as the input argument of your callback function
// It will be set to the observed future object. So that you may obtain
// the value of results()
qDebug() << future.results();
// Return another QFuture is possible.
return QtConcurrent::run(reducerFunc, future.results());
}).future();
// f2 is constructed before the QtConcurrent::run statement
// But its value is equal to the result of reducerFunc
More examples are available at : asyncfuture/example.cpp at master · benlau/asyncfuture
AsyncFuture is a single header library. You could just download the asyncfuture.h
in your source tree or install it via qpm
qpm install async.future.pri
or
wget https://raw.githubusercontent.com/benlau/asyncfuture/master/asyncfuture.h
This function creates an Observable<ARG> object which contains a future to represent the result of the signal. You could obtain the future by the future() method. And observe the result by subscribe() / context() methods
The ARG type is equal to the first parameter of the signal. If the signal does not contain any argument, ARG will be void. In case it has more than one argument, the rest will be ignored.
QFuture<void> f1 = observe(timer, &QTimer::timeout).future();
QFuture<bool> f2 = observe(button, &QAbstractButton::toggled).future();
See Observable<T>
This function creates an Observable object which provides an interface for observing the input future. See Observable<T>
// Convert a signal from QObject into QFuture
QFuture<bool> future = observe(button, &QAbstractButton::toggled).future();
// Listen from the future without using QFutureWatcher<T>
observe(future).subscribe([](bool toggled) {
// onCompleted. It is invoked when the observed future is finished successfully
qDebug() << "onCompleted";
},[]() {
// onCanceled
qDebug() << "onCancel";
});
This function creates a Combinator object (inherit Observable<void>
) for combining multiple future objects with different type into a single future.
For example:
QFuture<QImage> f1 = QtConcurrent::run(readImage, QString("image.jpg"));
QFuture<void> f2 = observe(timer, &QTimer::timeout).future();
QFuture<QImage> result = (combine(AllSettled) << f1 << f2).subscribe([=](){
// Read an image but do not return before timeout
return f1.result();
}).future();
Once all the observed futures finished, the contained future will be finished too. And it will be cancelled immediately if any one of the observed future is cancelled in fail fast mode. In case you wish the cancellation take place after all the futures finished, you should set mode to AllSettled
.
The deferred() function return a Deferred object that allows you to set QFuture completed/cancelled manually.
auto d = deferred<bool>();
d.subscribe([]() {
qDebug() << "onCompleted";
}, []() {
qDebug() << "onCancel";
});
d.complete(true); // or d.cancel();
See Deferred<T>
Obsevable is a chainable utility for observing a QFuture object. It is created by the observe() function. It can register multiple callbacks to be triggered in different situations. And that will create a new Observable / QFuture object to represent the result of the callback function. It may even call QtConcurrent::run() within the callback function to create a thread. Therefore, it could create a more complex/flexible workflow.
QFuture<T> future()
Obtain the QFuture object to represent the result.
Observable<T> subscribe(Completed onCompleted, Canceled onCanceled)
Observable<T> subscribe(Completed onCompleted);
Observable<T> subscribe(Completed onCompleted, Canceled onCanceled);
Register a onCompleted and/or onCanceled callback to the observed QFuture object. Unlike the context() function, the callbacks will be triggered as long as the current thread exists. The return value is an Observable object where R is the return type of the onCompleted callback.
Remarks: After v0.3.2, the callback will be executed in main thread.
QFuture<bool> future = observe(button, &QAbstractButton::toggled).future();
// Listen from the future without using QFutureWatcher<T>
observe(future).subscribe([](bool toggled) {
// onCompleted. It is invoked when the observed future is finished successfully
qDebug() << "onCompleted";
},[]() {
// onCanceled
qDebug() << "onCancel";
});
Observable<R> context(QObject* contextObject, Completed onCompleted)
This API is for advanced users only
Add a callback function that listens to the finished signal from the observing QFuture object. The callback won't be triggered if the future is cancelled.
The callback is invoked in the thread of the context object, In case the context object is destroyed before the finished signal, the callback function won't be triggered and the returned Observable object will cancel its future.
The return value is an Observable<R>
object where R is the return type of the onCompleted callback.
auto validator = [](QImage input) -> bool {
/* A dummy function. Return true for any case. */
return true;
};
QFuture<QImage> reading = QtConcurrent::run(readImage, QString("image.jpg"));
QFuture<bool> validating = observe(reading).context(contextObject, validator).future();
In the above example, the result of validating
is supposed to be true. However, if the contextObject
is destroyed before reading
future finished, it will be cancelled and the result will become undefined.
In subscribe() / context(), you may pass a function with zero or one argument as the onCompleted callback. If you give it an argument, the type must be either of T or QFuture. That would obtain the result or the observed future itself.
QFuture<QImage> reading = QtConcurrent::run(readImage, QString("image.jpg"));
observe(reading).subscribe([]() {
});
observe(reading).subscribe([](QImage result) {
});
observe(reading).subscribe([](QFuture<QImage> future) {
// In case you need to get future.results
});
The return type can be none or any kind of value. That would determine what type of Observable<R>
generated by context()/subscribe().
In case, you return a QFuture object. Then the new Observable<R>
object will be deferred to complete/cancel until your future object is resolved. Therefore, you could run QtConcurrent::run within your callback function to make a more complex/flexible multi-threading programming models.
QFuture<int> f1 = QtConcurrent::mapped(input, mapFunc);
QFuture<int> f2 = observe(f1).context(contextObject, [=](QFuture<int> future) {
// You may use QFuture as the input argument of your callback function
// It will be set to the observed future object. So that you may obtain
// the value of results()
qDebug() << future.results();
// Return another thread is possible.
return QtConcurrent::run(reducerFunc, future.results());
}).future();
// f2 is constructed before the QtConcurrent::run statement
// But its value is equal to the result of reducerFunc
The deferred<T>()
function return a Deferred object that allows you to manipulate a QFuture manually. The future() function return a running QFuture. You have to call Deferred.complete() / Deferred.cancel() to trigger the status changes.
The usage of complete/cancel in a Deferred object is pretty similar to the resolve/reject in a Promise object. You could complete a future by calling complete with a result value. If you give it another future, then it will observe the input future and change status once that is finished.
Auto Cancellation
The Deferred<T>
object is an explicitly shared class. You may own multiple copies and they are pointed to the same piece of shared data. In case, all of the instances are destroyed, it will cancel its future automatically.
But there has an exception if you have even called Deferred.complete(QFuture<T>
) / Deferred.cancel(QFuture<ANY>
) then it won't cancel its future due to destruction. That will leave to the observed future to determine the final state.
QFuture<void> future;
{
auto defer = deferred<void>();
future = defer.future();
}
QCOMPARE(future.isCanceled(), true); // Due to auto cancellation
QFuture<void> future;
{
auto defer = deferred<void>();
future = defer.future();
defer.complete(QtConcurrent::run(worker));
}
QCOMPARE(future.isCanceled(), false);
complete(T) / complete()
Complete this future object with the given arguments
complete(QList)
Complete the future object with a list of result. User may obtain all the value by QFuture::results().
complete(QFuture)
This future object is deferred to complete/cancel. It will adopt the state from the input future. If the input future is completed, then it will be completed too. That is same for cancel.
cancel()
Cancel the future object
cancel(QFuture)
This future object is deferred to cancel according to the input future. Once it is completed, this future will be cancelled. However, if the input future is cancelled. Then this future object will just ignore it. Unless it fulfils the auto-cancellation rule.
track(QFuture target)
Track the progress of target future.
It will set the progressMinimum and progressMaximum value from the target future. Moreover, it will bind the progressValueChanged signal and track the progress.
Remarks: It won't complete a future even the progressValue has been reached maximum
Added since v0.3.6
Example
auto defer = AsyncFuture::deferred<void>();
auto mappedFuture = QtConcurrent::mapped(data, workerFunc);
defer.track(mappedFuture);
AsyncFuture::observe(mappedFuture).subscribute([=]() mutable {
defer.complete();
});
return defer.future(); // It is a future with progress value same as the mappedFuture, but it don't contains the result.
There has few examples of different use-cases in this source file: