qt5base-lts/tests/auto/corelib/thread
Giuseppe D'Angelo 84fba93ebb QThread::create(): request interruption and join on destruction
If one destroys a running QThread, so far the behavior has been to crash
(à la std::thread) -- assuming the thread hasn't already signalled that
it has finished. This behavior is hostile to solutions such as using
QThread::create(), which always require a wait() before destroying the
thread object.

We can use the opportunity to change the behavior without breaking any
valid code. Instead of crashing, inside QThread's destructor we can ask
the new thread to quit, and then join it (à la std::jthread). This
simplifies the implementation of long-living runnables and the code that
manages them.

Deploying this solution for the whole QThread class may not be entirely
painless. While no correct code would work differently with the proposed
changes, incorrect code that deletes a running thread would no longer
crash "loudly" -- instead, it might deadlock "quietly", have memory
corruptions, etc.

Hence I'm limiting this approach to only the threads created by
QThread::create(), at least for the time being. This also side-steps
perhaps the biggest problem of generalizing the approach, which is that
placing such interrupt+join logic into~QThread's destructor would cause
it to be run _after_ a QThread subclass' own destructor has run,
destroying the subclass' data members too early. This might create
an antipattern if one chooses to subclass QThread. With create(), a
subclass in question exists, and it indeed has NSDMs, but it's entirely
under our control (in fact, I'm placing the logic just in its dtor).

[ChangeLog][QtCore][QThread] Destroying a QThread object created by
QThread::create() while the thread that it manages is still running will
now automatically ask that thread to quit, and will wait until the
thread has finished. Before, this resulted in a program crash. See the
documentation of QThread::~QThread() for more details.

Change-Id: Ib268b13da422e277ee3ed6f6c7b2ecc8cea5750c
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
2021-12-07 16:56:49 +01:00
..
qatomicint Remove the qmake project files 2021-01-07 15:32:28 +01:00
qatomicinteger Remove the qmake project files 2021-01-07 15:32:28 +01:00
qatomicpointer Remove the qmake project files 2021-01-07 15:32:28 +01:00
qfuture Fix QFuture continuations/handlers to work with move-only callables 2021-12-01 01:58:18 +01:00
qfuturesynchronizer Remove the qmake project files 2021-01-07 15:32:28 +01:00
qfuturewatcher Optimize QPromise destructor 2021-10-08 17:17:20 +02:00
qmutex tests: fix some -Wvolatile 2021-07-28 17:50:09 +02:00
qmutexlocker Remove the qmake project files 2021-01-07 15:32:28 +01:00
qpromise QFuture/QPromise: don't check for is_copy_constructible 2021-05-21 11:08:11 +02:00
qreadlocker Remove the qmake project files 2021-01-07 15:32:28 +01:00
qreadwritelock Skip tst_QReadWriteLock::multipleReadersLoop on QEMU 2021-09-09 21:08:42 +02:00
qresultstore Don't report results when the results list is empty 2021-07-27 19:57:27 +02:00
qsemaphore QSemaphore: add <chrono> overload of tryAcquire() 2021-06-30 23:48:21 +02:00
qthread QThread::create(): request interruption and join on destruction 2021-12-07 16:56:49 +01:00
qthreadonce Skip tst_qthreadonce test on QEMU 2021-07-20 16:51:51 +02:00
qthreadpool Consistently restore threadpool limit at end of tests 2021-10-22 09:43:42 +02:00
qthreadstorage Compile autotests for Integrity 2021-09-20 17:29:04 +03:00
qwaitcondition Remove the qmake project files 2021-01-07 15:32:28 +01:00
qwritelocker Remove the qmake project files 2021-01-07 15:32:28 +01:00
CMakeLists.txt Compile autotests for Integrity 2021-09-20 17:29:04 +03:00