Details
-
Task
-
Resolution: Done
-
P2: Important
-
None
-
None
-
None
-
8
-
Qt6_Foundation_Sprint 23, Qt6_Foundation_Sprint 24
Description
It seems to be a common scenario when users want to invoke continuations only within a specific context. For example:
static QFuture<void> DoSomething(); //// struct Foo : public QObject { void doSomethingAndDoSomethingElse() { QPointer<Foo> self(this); DoSomething().then([self] { if (self) { // do something with self. qDebug() << "Self is ok"; } else { qDebug() << "Foo already deleted"; } }); } }; Foo *foo = new Foo; foo->doSomethingAndDoSomethingElse(); // here foo gets deleted(by deleteLater for instance)
If DoSomething() finishes after foo is destroyed, the continuation attached via .then() shouldn't be invoked. The above example solves the problem by capturing the context and checking if it's valid. But this solution doesn't scale well if there are multiple continuations attached, instead of just a single one.
Another use-case is when the user needs to invoke the continuation in a specific object's context or thread.
Add a way of specifying the execution context to be used when invoking continuations.
For example;
QObject *obj;
future.then(object, [] {...})
// or
future.via(object).then([] {...})
See also the discussion here.