Commit 41d4fabf authored by Eike Ziller's avatar Eike Ziller

runAsync: Allow thread priority without thread pool

Change-Id: I6d4ee8fa1bf45c7a24ff6a004b3c69007c645547
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent af3029dc
......@@ -668,14 +668,12 @@ QFuture<ReduceResult> mapReduce(const Container &container, const InitFunction &
\sa std::thread
\sa std::invoke
*/
template <typename ResultType, typename Function, typename... Args,
typename = typename std::enable_if<
!std::is_same<typename std::decay<Function>::type, QThreadPool>::value
>::type>
QFuture<ResultType> runAsync(Function &&function, Args&&... args)
template <typename ResultType, typename Function, typename... Args>
QFuture<ResultType> runAsync(QThread::Priority priority, Function &&function, Args&&... args)
{
auto job = new Internal::AsyncJob<ResultType,Function,Args...>
(std::forward<Function>(function), std::forward<Args>(args)...);
job->setThreadPriority(priority);
QFuture<ResultType> future = job->future();
auto thread = new Internal::RunnableThread(job);
thread->moveToThread(qApp->thread()); // make sure thread gets deleteLater on main thread
......@@ -684,6 +682,17 @@ QFuture<ResultType> runAsync(Function &&function, Args&&... args)
return future;
}
template <typename ResultType, typename Function, typename... Args,
typename = typename std::enable_if<
!std::is_same<typename std::decay<Function>::type, QThreadPool>::value
&& !std::is_same<typename std::decay<Function>::type, QThread::Priority>::value
>::type>
QFuture<ResultType> runAsync(Function &&function, Args&&... args)
{
return runAsync<ResultType>(QThread::InheritPriority, std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args>
QFuture<ResultType> runAsync(QThreadPool *pool, QThread::Priority priority,
Function &&function, Args&&... args)
......
......@@ -41,6 +41,7 @@ private slots:
#ifdef SUPPORTS_MOVE
void moveOnlyType();
#endif
void threadPriority();
};
void report3(QFutureInterface<int> &fi)
......@@ -304,6 +305,18 @@ void tst_RunExtensions::moveOnlyType()
#endif
void tst_RunExtensions::threadPriority()
{
QScopedPointer<QThreadPool> pool(new QThreadPool);
// with pool
QCOMPARE(Utils::runAsync<int>(pool.data(), QThread::LowestPriority, &report3).results(),
QList<int>({0, 2, 1}));
// without pool
QCOMPARE(Utils::runAsync<int>(QThread::LowestPriority, report3).results(),
QList<int>({0, 2, 1}));
}
QTEST_MAIN(tst_RunExtensions)
#include "tst_runextensions.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment