summaryrefslogtreecommitdiffstats
path: root/src/concurrent/doc/snippets
diff options
context:
space:
mode:
authorVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-02-26 15:22:40 +0100
committerVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-03-11 14:46:25 +0100
commitc977e74afd18afff8729070f631e6b7a3f2887f5 (patch)
tree08be91368aaee6391e1fcb71f67f90708f7b9e7a /src/concurrent/doc/snippets
parent01bacdf7abb071198d843acdfb22ce1701766be8 (diff)
QtConcurrent::run: accept more then five function's arguments
[ChangeLog][Potentially Source-Incompatible Changes] QtConcurrent::run has the following signatures: run(Function &&f, Args &&...args) and run(QThreadPool *pool, Function &&f, Args &&...args). If f is a member pointer, the first argument of args should be an object for which that member is defined (or a reference, or a pointer to it). See the documentation for more details. Fixes: QTBUG-82383 Change-Id: I18f7fcfb2adbdd9f75b29c346bd3516304e32d31 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/concurrent/doc/snippets')
-rw-r--r--src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp
index 5437822842..0b82766054 100644
--- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp
+++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp
@@ -93,7 +93,7 @@ QString result = future.result();
//! [4]
// call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread
QByteArray bytearray = "hello world";
-QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ',');
+QFuture<QList<QByteArray> > future = QtConcurrent::run(&QByteArray::split, bytearray, ',');
...
QList<QByteArray> result = future.result();
//! [4]
@@ -101,16 +101,46 @@ QList<QByteArray> result = future.result();
//! [5]
// call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
QImage image = ...;
-QFuture<void> future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba);
+QFuture<void> future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba);
...
future.waitForFinished();
// At this point, the pixels in 'image' have been inverted
//! [5]
-
//! [6]
QFuture<void> future = QtConcurrent::run([=]() {
// Code in this block will run in another thread
});
...
//! [6]
+
+//! [7]
+static void addOne(int &n) { ++n; }
+...
+int n = 42;
+QtConcurrent::run(&addOne, std::ref(n)).waitForFinished(); // n == 43
+//! [7]
+
+//! [8]
+struct TestClass
+{
+ void operator()(int s1) { s = s1; }
+ int s = 42;
+};
+
+...
+
+TestClass o;
+
+// Modify original object
+QtConcurrent::run(std::ref(o), 15).waitForFinished(); // o.s == 15
+
+// Modify a copy of the original object
+QtConcurrent::run(o, 42).waitForFinished(); // o.s == 15
+
+// Use a temporary object
+QtConcurrent::run(TestClass(), 42).waitForFinished();
+
+// Ill-formed
+QtConcurrent::run(&o, 42).waitForFinished(); // compilation error
+//! [8]