path: root/tests/auto/corelib/thread/qthread/
diff options
authorGiuseppe D'Angelo <>2017-04-03 13:52:20 +0100
committerGiuseppe D'Angelo <>2017-04-24 15:24:22 +0000
commit597d4ff7962c0add87e4b93da4c366503d11aff5 (patch)
tree7d90570c83e914efab1ff9adf40e505e2c9611a6 /tests/auto/corelib/thread/qthread/
parent711e94916ad4b97128849072caf977c13152c64c (diff)
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 <>
Diffstat (limited to 'tests/auto/corelib/thread/qthread/')
1 files changed, 2 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qthread/ b/tests/auto/corelib/thread/qthread/
index 18d867ecef..381f6c9e45 100644
--- a/tests/auto/corelib/thread/qthread/
+++ b/tests/auto/corelib/thread/qthread/
@@ -2,3 +2,5 @@ CONFIG += testcase
TARGET = tst_qthread
QT = core testlib
SOURCES = tst_qthread.cpp
+qtConfig(c++14):CONFIG += c++14
+qtConfig(c++1z):CONFIG += c++1z