qt5base-lts/tests/auto/corelib/thread
Giuseppe D'Angelo 597d4ff796 QThread: add static create function
In the spirit of std::thread, which takes a function to call and its
parameters, and runs it in a new thread. Since the user might want to
connect to signals, move QObjects into the new thread, etc., the new
thread is not immediately started.

Although technically all of this _should_ be implementable in pure
C++11, there is nothing in the Standard to help us not reinvent all the
plumbing: packing the decay'd parameters, storing them, invoking the
function over the parameters (honoring INVOKE/std::invoke semantics).
std::function does not do the job, as it's copiable and therefore does
not support move-only functors; std::bind does not have INVOKE
semantics.

I certainly do not want to reimplement all the required facilities
inside of Qt. Therefore, the full blown implementation requires C++17
(std::invoke).

In order to make this useful also in pre-C++17, there are two additional
implementations (C++11 and C++14) that support just a callable, without
any arguments passed to it. The C++11 implementation makes use of a
class to store and call the callable (even move-only ones); basically,
it's what a closure type for a C++14 lambda would look like.

An alternative implementation could've used some of the existing
facilities inside QObject::connect implementation that store a functor
(for the connect() overload connecting to free functions), namely:
the QtPrivate::QFunctorSlotObject class. However:

* QFunctorSlotObject does not support move-only callables (see
QTBUG-60339);
* QFunctorSlotObject itself is not a callable (apparently by design),
and requires to be wrapped in a lambda that calls call() on it;
* the moment QTBUG-60339 is solved, we'd need the same handwritten
closure to keep QFunctorSlotObject working with move-only callabes.

So: just use the handwritten one.

The C++14 implementation is a simplified version of the C++11 one,
actually using a generalized lambda capture (corresponding to the
handwritten C++11 closure type).

All three implementations use std::async (with a deferred launch policy,
a nice use case for it!) under the hood. It's certainly an overkill for
our use case, as we don't need the std::future, but at least std::async
does all the plumbing for us.

[ChangeLog][QtCore][QThread] Added the QThread::create function.

Change-Id: I339d0be6f689df7d56766839baebda0aa2f7e94c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2017-04-24 15:24:22 +00:00
..
qatomicint Add Intel copyright to files that Intel has had non-trivial contribution 2016-01-21 22:44:21 +00:00
qatomicinteger Merge remote-tracking branch 'origin/5.6' into 5.7 2016-03-24 20:37:33 +01:00
qatomicpointer Add Intel copyright to files that Intel has had non-trivial contribution 2016-01-21 22:44:21 +00:00
qfuture Merge remote-tracking branch 'origin/5.9' into dev 2017-03-20 09:00:44 +01:00
qfuturesynchronizer Drop unnecessary dependencies from some tests 2017-01-10 16:34:52 +00:00
qfuturewatcher Updated license headers 2016-01-21 18:55:18 +00:00
qmutex Merge remote-tracking branch 'origin/5.8' into 5.9 2017-03-13 15:55:44 +01:00
qmutexlocker Updated license headers 2016-01-21 18:55:18 +00:00
qreadlocker Updated license headers 2016-01-21 18:55:18 +00:00
qreadwritelock Updated license headers 2016-01-21 18:55:18 +00:00
qresultstore Fix UB in QFutureInterface: invalid casts from ResultStoreBase to ResultStore<> 2017-03-06 18:32:28 +00:00
qsemaphore Merge remote-tracking branch 'origin/5.9' into dev 2017-03-20 09:00:44 +01:00
qthread QThread: add static create function 2017-04-24 15:24:22 +00:00
qthreadonce QtCore: Remove Windows CE. 2016-03-30 11:22:47 +00:00
qthreadpool Use new QSemaphoreReleaser in tst_QThreadPool 2017-03-09 19:04:33 +00:00
qthreadstorage Merge remote-tracking branch 'origin/5.8' into 5.9 2017-03-13 15:55:44 +01:00
qwaitcondition Blacklist tst_QWaitCondition::wakeOne() on Windows 2017-02-14 06:51:52 +00:00
qwritelocker Updated license headers 2016-01-21 18:55:18 +00:00
thread.pro Build examples and tests only if their requirements are met 2017-03-22 15:55:55 +00:00