diff options
author | Liang Qi <liang.qi@qt.io> | 2017-09-26 16:08:55 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2017-09-26 16:14:54 +0200 |
commit | aadfe7d634de04519102c5827ca885dc2e2199c9 (patch) | |
tree | d92db346ca95332b177036a53f1f6beb2e24fb74 /src/corelib/thread/qthread.h | |
parent | 4b6c1448047362b8c38d265e6414f0e3e59b8d37 (diff) | |
parent | a732e16d5fd9dbf8a0289fec9f948b12e9ba2c19 (diff) |
Merge remote-tracking branch 'origin/5.10' into dev
Conflicts:
src/gui/kernel/qguiapplication.cpp
src/platformsupport/input/libinput/qlibinputpointer.cpp
src/plugins/platforminputcontexts/ibus/qibusplatforminputcontext.h
src/plugins/platforms/cocoa/qcocoawindow.h
src/testlib/qtestsystem.h
Change-Id: I5975ffb3261c2dd82fe02ec4e57df7c0950226c5
Diffstat (limited to 'src/corelib/thread/qthread.h')
-rw-r--r-- | src/corelib/thread/qthread.h | 128 |
1 files changed, 59 insertions, 69 deletions
diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index b1d02a2b75..d18152a52d 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -119,6 +119,12 @@ public: bool event(QEvent *event) override; int loopLevel() const; +#ifdef Q_QDOC + template <typename Function, typename... Args> + static QThread *create(Function &&f, Args &&... args); + template <typename Function> + static QThread *create(Function &&f); +#else #ifdef QTHREAD_HAS_CREATE #ifdef QTHREAD_HAS_VARIADIC_CREATE template <typename Function, typename... Args> @@ -128,6 +134,7 @@ public: static QThread *create(Function &&f); #endif #endif +#endif public Q_SLOTS: void start(Priority = InheritPriority); @@ -158,98 +165,81 @@ protected: private: Q_DECLARE_PRIVATE(QThread) +#ifdef QTHREAD_HAS_CREATE + static QThread *createThreadImpl(std::future<void> &&future); +#endif + friend class QCoreApplication; friend class QThreadData; }; #ifdef QTHREAD_HAS_CREATE -namespace QtPrivate { -class QThreadCreateThread : public QThread +#ifdef QTHREAD_HAS_VARIADIC_CREATE +// C++17: std::thread's constructor complying call +template <typename Function, typename... Args> +QThread *QThread::create(Function &&f, Args &&... args) { -public: -#if defined(QTHREAD_HAS_VARIADIC_CREATE) - // C++17: std::thread's constructor complying call - template <typename Function, typename... Args> - explicit QThreadCreateThread(Function &&f, Args &&... args) - : m_future(std::async(std::launch::deferred, - [f = static_cast<typename std::decay<Function>::type>(std::forward<Function>(f))](auto &&... largs) mutable -> void - { - (void)std::invoke(std::move(f), std::forward<decltype(largs)>(largs)...); - }, std::forward<Args>(args)...)) - { - } -#elif defined(__cpp_init_captures) && __cpp_init_captures >= 201304 - // C++14: implementation for just one callable - template <typename Function> - explicit QThreadCreateThread(Function &&f) - : m_future(std::async(std::launch::deferred, - [f = static_cast<typename std::decay<Function>::type>(std::forward<Function>(f))]() mutable -> void - { - (void)f(); - })) - { - } -#else -private: - // C++11: same as C++14, but with a workaround for not having generalized lambda captures - template <typename Function> - struct Callable - { - explicit Callable(Function &&f) - : m_function(std::forward<Function>(f)) + using DecayedFunction = typename std::decay<Function>::type; + auto threadFunction = + [f = static_cast<DecayedFunction>(std::forward<Function>(f))](auto &&... largs) mutable -> void { - } - -#if defined(Q_COMPILER_DEFAULT_MEMBERS) && defined(Q_COMPILER_DELETE_MEMBERS) - // Apply the same semantics of a lambda closure type w.r.t. the special - // member functions, if possible: delete the copy assignment operator, - // bring back all the others as per the RO5 (cf. §8.1.5.1/11 [expr.prim.lambda.closure]) - ~Callable() = default; - Callable(const Callable &) = default; - Callable(Callable &&) = default; - Callable &operator=(const Callable &) = delete; - Callable &operator=(Callable &&) = default; -#endif + (void)std::invoke(std::move(f), std::forward<decltype(largs)>(largs)...); + }; - void operator()() + return createThreadImpl(std::async(std::launch::deferred, + std::move(threadFunction), + std::forward<Args>(args)...)); +} +#elif defined(__cpp_init_captures) && __cpp_init_captures >= 201304 +// C++14: implementation for just one callable +template <typename Function> +QThread *QThread::create(Function &&f) +{ + using DecayedFunction = typename std::decay<Function>::type; + auto threadFunction = + [f = static_cast<DecayedFunction>(std::forward<Function>(f))]() mutable -> void { - (void)m_function(); - } - - typename std::decay<Function>::type m_function; - }; + (void)f(); + }; -public: - template <typename Function> - explicit QThreadCreateThread(Function &&f) - : m_future(std::async(std::launch::deferred, Callable<Function>(std::forward<Function>(f)))) + return createThreadImpl(std::async(std::launch::deferred, std::move(threadFunction))); +} +#else +// C++11: same as C++14, but with a workaround for not having generalized lambda captures +namespace QtPrivate { +template <typename Function> +struct Callable +{ + explicit Callable(Function &&f) + : m_function(std::forward<Function>(f)) { } -#endif // QTHREAD_HAS_VARIADIC_CREATE -private: - void run() override +#if defined(Q_COMPILER_DEFAULT_MEMBERS) && defined(Q_COMPILER_DELETE_MEMBERS) + // Apply the same semantics of a lambda closure type w.r.t. the special + // member functions, if possible: delete the copy assignment operator, + // bring back all the others as per the RO5 (cf. §8.1.5.1/11 [expr.prim.lambda.closure]) + ~Callable() = default; + Callable(const Callable &) = default; + Callable(Callable &&) = default; + Callable &operator=(const Callable &) = delete; + Callable &operator=(Callable &&) = default; +#endif + + void operator()() { - m_future.get(); + (void)m_function(); } - std::future<void> m_future; + typename std::decay<Function>::type m_function; }; - } // namespace QtPrivate -#ifdef QTHREAD_HAS_VARIADIC_CREATE -template <typename Function, typename... Args> -QThread *QThread::create(Function &&f, Args &&... args) -{ - return new QtPrivate::QThreadCreateThread(std::forward<Function>(f), std::forward<Args>(args)...); -} -#else template <typename Function> QThread *QThread::create(Function &&f) { - return new QtPrivate::QThreadCreateThread(std::forward<Function>(f)); + return createThreadImpl(std::async(std::launch::deferred, QtPrivate::Callable<Function>(std::forward<Function>(f)))); } #endif // QTHREAD_HAS_VARIADIC_CREATE |