From a8261e327d6316d944cfcc312a1b2e71bd4e5d27 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 23 Dec 2022 16:23:45 +0100 Subject: [PATCH] QPromise: improve documentation snippet The multi-thread snippet in the documentation, when copied as is, could actually crash because of the race condition between the main thread and the thread that generate results for the promise. This is fixed by explicitly calling QPromise::start(). Actually, the underlying snippet already has this call, it just was not included in the documentation. This patch modifies the documentation snippet to include calls to both QPromise::start() and QPromise::finish(). Fixes: QTBUG-109230 Pick-to: 6.5 6.4 6.2 Change-Id: Ic25f31a6b3b16ba6bc06a0b199289c8c5d50bab6 Reviewed-by: Sona Kurazyan --- src/corelib/thread/qpromise.qdoc | 2 ++ tests/auto/corelib/thread/qpromise/snippet_qpromise.cpp | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/corelib/thread/qpromise.qdoc b/src/corelib/thread/qpromise.qdoc index 144ad169741..37ffa8485e0 100644 --- a/src/corelib/thread/qpromise.qdoc +++ b/src/corelib/thread/qpromise.qdoc @@ -38,6 +38,8 @@ \snippet snippet_qpromise.cpp multithread_init \codeline \snippet snippet_qpromise.cpp multithread_main + \codeline + \snippet snippet_qpromise.cpp multithread_cleanup \sa QFuture */ diff --git a/tests/auto/corelib/thread/qpromise/snippet_qpromise.cpp b/tests/auto/corelib/thread/qpromise/snippet_qpromise.cpp index e87ba82309d..a1c7da280e1 100644 --- a/tests/auto/corelib/thread/qpromise/snippet_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/snippet_qpromise.cpp @@ -53,9 +53,9 @@ void snippet_QPromise::multithreadExample() QFuture future = sharedPromise->future(); // ... -//! [multithread_init] sharedPromise->start(); +//! [multithread_init] //! [multithread_main] // here, QPromise is shared between threads via a smart pointer @@ -88,7 +88,9 @@ void snippet_QPromise::multithreadExample() for (auto& t : threads) t->wait(); +//! [multithread_cleanup] sharedPromise->finish(); +//! [multithread_cleanup] #endif }