diff --git a/src/concurrent/qtconcurrentrunbase.h b/src/concurrent/qtconcurrentrunbase.h index 6038d0e60c..4870362146 100644 --- a/src/concurrent/qtconcurrentrunbase.h +++ b/src/concurrent/qtconcurrentrunbase.h @@ -93,7 +93,13 @@ public: promise.setRunnable(this); promise.reportStarted(); QFuture theFuture = promise.future(); - parameters.threadPool->start(this, parameters.priority); + + if (parameters.threadPool) { + parameters.threadPool->start(this, parameters.priority); + } else { + promise.reportCanceled(); + promise.reportFinished(); + } return theFuture; } diff --git a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp index 235ffca0fc..0ff98c42f5 100644 --- a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp +++ b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp @@ -65,6 +65,7 @@ private slots: void crefFunction(); void customPromise(); void nonDefaultConstructibleValue(); + void nullThreadPool(); }; void light() @@ -1577,5 +1578,16 @@ void tst_QtConcurrentRun::nonDefaultConstructibleValue() QCOMPARE(future.result().value, 42); } +// QTBUG-98901 +void tst_QtConcurrentRun::nullThreadPool() +{ + QThreadPool *pool = nullptr; + std::atomic isInvoked = false; + auto future = run(pool, [&] { isInvoked = true; }); + future.waitForFinished(); + QVERIFY(future.isCanceled()); + QVERIFY(!isInvoked); +} + QTEST_MAIN(tst_QtConcurrentRun) #include "tst_qtconcurrentrun.moc"