31 #include <QMutexLocker>
33 #include <QWaitCondition>
36 #include <QFutureSynchronizer>
44 void runLightFunction();
45 void runHeavyFunction();
47 void reportValueWithPromise();
48 void functionObject();
49 void memberFunctions();
50 void implicitConvertibleTypes();
52 void pollForIsFinished();
54 #ifndef QT_NO_EXCEPTIONS
56 void unhandledException();
60 void callableObjectWithState();
62 void withPromiseInThreadPool();
63 void withPromiseAndThen();
67 void nonDefaultConstructibleValue();
68 void nullThreadPool();
69 void nullThreadPoolNoLeak();
86 qDebug(
"in function with arg");
92 qDebug(
"in function with promise");
98 qDebug(
"in function with promise and with arg");
106 for (
int i = 0;
i < 1000000; ++
i)
111 void tst_QtConcurrentRun::runLightFunction()
113 qDebug(
"starting function");
120 qDebug(
"starting function");
127 qDebug(
"starting function with arg");
134 qDebug(
"starting function with promise");
141 qDebug(
"starting function with promise and with arg");
148 void tst_QtConcurrentRun::runHeavyFunction()
151 qDebug(
"starting function");
208 void tst_QtConcurrentRun::returnValue()
338 f =
run(aNoExcept, 20);
370 f =
run(aConstNoExcept);
380 f =
run(aConstNoExcept, 20);
437 void tst_QtConcurrentRun::reportValueWithPromise()
569 f =
run(aConstNoExcept);
596 void tst_QtConcurrentRun::functionObject()
625 void tst_QtConcurrentRun::memberFunctions()
680 void tst_QtConcurrentRun::implicitConvertibleTypes()
705 void tst_QtConcurrentRun::runWaitLoop()
707 for (
int i = 0;
i < 1000; ++
i)
708 run(
fn).waitForFinished();
714 return std::find_if(futures.cbegin(), futures.cend(), hasNotFinished)
715 == futures.constEnd();
718 static void runFunction()
725 void tst_QtConcurrentRun::pollForIsFinished()
731 for (
int i = 0;
i < numThreads; ++
i)
763 void tst_QtConcurrentRun::recursive()
768 count.storeRelaxed(0);
775 count.storeRelaxed(0);
814 #ifndef QT_NO_EXCEPTIONS
839 void tst_QtConcurrentRun::exceptions()
851 QFAIL(
"did not get exception");
860 QFAIL(
"did not get exception");
869 QFAIL(
"did not get exception");
878 QFAIL(
"did not get exception");
886 QVERIFY2(caught,
"did not get exception");
902 QVERIFY2(caught,
"did not get exception");
905 void tst_QtConcurrentRun::unhandledException()
915 std::rethrow_exception(
e.exception());
916 }
catch (
const Exception &) {
949 void tst_QtConcurrentRun::functor()
977 QtConcurrent::run<int>(ofWithPromise).waitForFinished();
979 QtConcurrent::run<int>(ofWithPromise, 1).waitForFinished();
1006 void tst_QtConcurrentRun::lambda()
1013 {
return a +
b; }, 1, 2, 3, 4, 5).
result(), 1 + 5);
1032 {
return a +
b; }, 12, 15).
result(),
double(12 + 15));
1034 {
return a +
b; }, 1, 2, 3, 4, 5).
result(), 1 + 5);
1062 void tst_QtConcurrentRun::callableObjectWithState()
1109 for (
int i = 0;
i <
n; ++
i)
1127 for (
int i = 0;
i <
n; ++
i)
1143 for (
int i = 0;
i <
n; ++
i)
1156 for (
int i = 0;
i <
n; ++
i)
1178 void tst_QtConcurrentRun::withPromise()
1215 for (
int i = 0;
i <
n; ++
i)
1222 for (
int i = 0;
i <
n; ++
i)
1268 void tst_QtConcurrentRun::withPromiseInThreadPool()
1306 for (int i = 0; i < n; ++i)
1307 promise.addResult(0);
1313 for (
int i = 0;
i <
n; ++
i)
1358 void tst_QtConcurrentRun::withPromiseAndThen()
1360 bool runExecuted =
false;
1361 bool cancelReceivedBeforeSync =
false;
1362 bool cancelReceivedAfterSync =
false;
1364 bool syncBegin =
false;
1365 bool syncEnd =
false;
1370 auto reset = [&]() {
1371 runExecuted =
false;
1372 cancelReceivedBeforeSync =
false;
1373 cancelReceivedAfterSync =
false;
1392 cancelReceivedBeforeSync = promise.
isCanceled();
1395 waitForFlag(syncEnd);
1397 cancelReceivedAfterSync = promise.
isCanceled();
1398 if (cancelReceivedAfterSync)
1406 waitForFlag(syncBegin);
1413 QVERIFY(!cancelReceivedBeforeSync);
1414 QVERIFY(cancelReceivedAfterSync);
1420 bool thenExecuted =
false;
1421 bool cancelExecuted =
false;
1424 .onCanceled([&]() { cancelExecuted =
true; });
1426 waitForFlag(syncBegin);
1430 resultFuture.waitForFinished();
1436 QVERIFY(!cancelReceivedBeforeSync);
1437 QVERIFY(!cancelReceivedAfterSync);
1443 bool thenExecuted =
false;
1444 bool cancelExecuted =
false;
1447 .onCanceled([&]() { cancelExecuted =
true; });
1449 waitForFlag(syncBegin);
1453 resultFuture.waitForFinished();
1458 QVERIFY(!cancelReceivedBeforeSync);
1459 QVERIFY(cancelReceivedAfterSync);
1482 void tst_QtConcurrentRun::moveOnlyType()
1488 void tst_QtConcurrentRun::crefFunction()
1496 auto lambda = [](
int n) {
1525 for (
int i = 0;
i <
n; ++
i)
1534 for (
int i = 0;
i <
n; ++
i)
1560 void tst_QtConcurrentRun::customPromise()
1566 QCOMPARE(
p.future().progressMinimum(), -10);
1567 QCOMPARE(
p.future().progressMaximum(), 10);
1570 void tst_QtConcurrentRun::nonDefaultConstructibleValue()
1572 struct NonDefaultConstructible
1574 explicit NonDefaultConstructible(
int v) :
value(
v) { }
1583 void tst_QtConcurrentRun::nullThreadPool()
1586 std::atomic<bool> isInvoked =
false;
1605 void tst_QtConcurrentRun::nullThreadPoolNoLeak()
1616 #include "tst_qtconcurrentrun.moc"
small capitals from c petite p scientific i
[1]
int member1(int in) const
int operator()(int in) const
int member1(int in) const noexcept
int operator()(int in) const noexcept
int member0() const noexcept
int operator()() const noexcept
void operator()(QPromise< int > &promise) const noexcept
void member1(QPromise< int > &promise, int in) const noexcept
void member0(QPromise< int > &promise) const noexcept
void member1(QPromise< int > &promise, int in) const
void operator()(QPromise< int > &promise) const
void member0(QPromise< int > &promise) const
int operator()(int in) noexcept
int operator()() noexcept
int member1(int in) noexcept
void member0(QPromise< int > &promise) noexcept
void member1(QPromise< int > &promise, int in) noexcept
void operator()(QPromise< int > &promise) noexcept
void member0(QPromise< int > &promise)
void operator()(QPromise< int > &promise)
void member1(QPromise< int > &promise, int in)
void operator()(QPromise< double > &promise, int n) const
void operator()(QPromise< int > &promise, const MoveOnlyType &)
MoveOnlyType & operator=(MoveOnlyType &&)=default
MoveOnlyType & operator=(const MoveOnlyType &)=delete
MoveOnlyType(MoveOnlyType &&)=default
MoveOnlyType(const MoveOnlyType &)=delete
void member0(QPromise< double > &promise) const
void memberString2(QPromise< QString > &promise, QString s) const
static void staticMember0(QPromise< double > &promise)
static void staticMember1(QPromise< double > &promise, int n)
void member1(QPromise< double > &promise, int n) const
void nonConstMember(QPromise< double > &promise)
void memberString1(QPromise< QString > &promise, const QString &s) const
The QAtomicInt class provides platform-independent atomic operations on int.
void storeRelaxed(T newValue) noexcept
The QEventLoop class provides a means of entering and leaving an event loop.
int exec(ProcessEventsFlags flags=AllEvents)
The QException class provides a base class for exceptions that can be transferred across threads.
QList< T > results() const
QFuture< ResultType< Function > > then(Function &&function)
void addFuture(const QFuture< T > &future)
QList< QFuture< T > > futures() const
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
The QMutex class provides access serialization between threads.
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.
void setProgressRange(int minimum, int maximum)
bool addResult(U &&result, int index=-1)
The QRunnable class is the base class for all runnable objects.
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
The QString class provides a Unicode character string.
QStringList split(const QString &sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QString & append(QChar c)
static int idealThreadCount() noexcept
static QThread * currentThread()
static void msleep(unsigned long)
The QThreadPool class manages a collection of QThreads.
void start(QRunnable *runnable, int priority=0)
static QThreadPool * globalInstance()
void setMaxThreadCount(int maxThreadCount)
bool singleShot
whether the timer is a single-shot timer
The QUnhandledException class represents an unhandled exception in a Qt Concurrent worker thread.
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
auto it unsigned count const
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
auto run(QThreadPool *pool, Function &&f, Args &&...args)
QList< QString > QStringList
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
EGLOutputLayerEXT EGLint EGLAttrib value
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLenum GLuint GLint level
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLenum GLsizei count
GLenum GLenum GLenum input
GLsizei const GLchar *const * string
[0]
#define QTEST_MAIN(TestObject)
#define QVERIFY(statement)
#define QTRY_VERIFY(expr)
#define QVERIFY2(statement, description)
QFuture< void > future
[5]
static constexpr int defaultState()
void setNewState(int newState)
int operator()(int newState)
static constexpr int defaultState()
void setNewState(QPromise< int > &, int newState)
void operator()(QPromise< int > &promise, int newState)
void operator()(int, int, int, int, int)
void operator()(int, int, int, int, int, int)
int operator()(int a, int b)
double operator()(double a, double b)
void operator()(int, int, int)
void operator()(int, int, int, int)
void operator()(QPromise< int > &, double)
static std::atomic< int > count
LifetimeChecker(const LifetimeChecker &)
void operator()(QPromise< double > &)
void operator()(QPromise< double > &, int)
void operator()(QPromise< int > &, int)
void operator()(QPromise< int > &)
void operator()(int) const
void stringRefFunction(QString &)
void doubleFunction(double)
void recursiveRun(int level)
void reportString2(QPromise< QString > &promise, QString s)
void stringIntFunction(QString)
void reportString1(QPromise< QString > &promise, const QString &s)
void reportN(QPromise< double > &promise, int n)
int throwFunctionReturn()
void report3(QPromise< int > &promise)
int recursiveResult(int level)
void reportInt0(QPromise< int > &promise)
void reportIntPlusOne(QPromise< int > &promise, int i)
void stringFunction(QString)
int explicitPromise(QPromise< int > &promise, int &i)
void stringConstRefFunction(const QString &)