[docs] QPromise: don't recommend legacy smart pointers

Use unique_ptr instead of QScopedPointer and shared_ptr instead of
QSharedPointer.

Amends 68de38ded1c0e5387ae29aacaee50ba5dacfc59a.

Pick-to: 6.8 6.5 6.2
Change-Id: Ic5c4c4962be69aecd4312dbdbfa84deb8a3c1e57
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Marc Mutz 2024-11-01 11:09:07 +01:00
parent cb9d8226d7
commit 076445ec0d

View File

@ -11,8 +11,8 @@
#include <qfuture.h> #include <qfuture.h>
#include <qfuturewatcher.h> #include <qfuturewatcher.h>
#include <qpromise.h> #include <qpromise.h>
#include <qscopedpointer.h>
#include <qsharedpointer.h> #include <memory>
class snippet_QPromise class snippet_QPromise
{ {
@ -29,7 +29,7 @@ void snippet_QPromise::basicExample()
QPromise<int> promise; QPromise<int> promise;
QFuture<int> future = promise.future(); QFuture<int> future = promise.future();
QScopedPointer<QThread> thread(QThread::create([] (QPromise<int> promise) { const std::unique_ptr<QThread> thread(QThread::create([] (QPromise<int> promise) {
promise.start(); // notifies QFuture that the computation is started promise.start(); // notifies QFuture that the computation is started
promise.addResult(42); promise.addResult(42);
promise.finish(); // notifies QFuture that the computation is finished promise.finish(); // notifies QFuture that the computation is finished
@ -49,7 +49,7 @@ void snippet_QPromise::multithreadExample()
{ {
#if QT_CONFIG(cxx11_future) #if QT_CONFIG(cxx11_future)
//! [multithread_init] //! [multithread_init]
QSharedPointer<QPromise<int>> sharedPromise(new QPromise<int>()); const auto sharedPromise = std::make_shared<QPromise<int>>();
QFuture<int> future = sharedPromise->future(); QFuture<int> future = sharedPromise->future();
// ... // ...
@ -59,14 +59,14 @@ void snippet_QPromise::multithreadExample()
//! [multithread_main] //! [multithread_main]
// here, QPromise is shared between threads via a smart pointer // here, QPromise is shared between threads via a smart pointer
QScopedPointer<QThread> threads[] = { const std::unique_ptr<QThread> threads[] = {
QScopedPointer<QThread>(QThread::create([] (auto sharedPromise) { std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
sharedPromise->addResult(0, 0); // adds value 0 by index 0 sharedPromise->addResult(0, 0); // adds value 0 by index 0
}, sharedPromise)), }, sharedPromise)),
QScopedPointer<QThread>(QThread::create([] (auto sharedPromise) { std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
sharedPromise->addResult(-1, 1); // adds value -1 by index 1 sharedPromise->addResult(-1, 1); // adds value -1 by index 1
}, sharedPromise)), }, sharedPromise)),
QScopedPointer<QThread>(QThread::create([] (auto sharedPromise) { std::unique_ptr<QThread>(QThread::create([] (auto sharedPromise) {
sharedPromise->addResult(-2, 2); // adds value -2 by index 2 sharedPromise->addResult(-2, 2); // adds value -2 by index 2
}, sharedPromise)), }, sharedPromise)),
// ... // ...
@ -104,7 +104,7 @@ void snippet_QPromise::suspendExample()
promise.start(); promise.start();
// Start a computation thread that supports suspension and cancellation // Start a computation thread that supports suspension and cancellation
QScopedPointer<QThread> thread(QThread::create([] (QPromise<int> promise) { const std::unique_ptr<QThread> thread(QThread::create([] (QPromise<int> promise) {
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
promise.addResult(i); promise.addResult(i);
promise.suspendIfRequested(); // support suspension promise.suspendIfRequested(); // support suspension