16#ifndef QGSTHREADINGUTILS_H
17#define QGSTHREADINGUTILS_H
55 template <
typename Func>
60 if ( QThread::currentThread() == qApp->thread() )
72 QSemaphore semaphoreMainThreadReady( 1 );
77 QSemaphore semaphoreWorkerThreadReady( 1 );
81 semaphoreMainThreadReady.acquire();
82 semaphoreWorkerThreadReady.acquire();
84 const std::function<void()> waitFunc = [&semaphoreMainThreadReady, &semaphoreWorkerThreadReady]()
89 semaphoreMainThreadReady.release();
92 semaphoreWorkerThreadReady.acquire();
95 QMetaObject::invokeMethod( qApp, waitFunc, Qt::QueuedConnection );
99 while ( !semaphoreMainThreadReady.tryAcquire( 1, 100 ) )
101 if ( feedback->isCanceled() )
103 semaphoreWorkerThreadReady.release();
113 semaphoreWorkerThreadReady.release();
116 QMetaObject::invokeMethod( qApp, func, Qt::BlockingQueuedConnection );
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Provides threading utilities for QGIS.
static bool runOnMainThread(const Func &func, QgsFeedback *feedback=nullptr)
Guarantees that func is executed on the main thread.