Thanks in advance.
We are calling some third party API's from our Qt application. As a precautionary measure we are calling them from QtConcurrentRun thread and making main thread wait for a definite timeout using QWaitCondition wait(). The same locked muted that is passed to QWaitCondition wait() is used for locking inside the worker thread function. However, the wait finishes only after the worker thread has finished and signaled using wakeAll(). Consequently, if the worker thread hangs, our main thread also hangs, which defeats our purpose. Is there an alternate way to achieve this?
MyClass:startBackgroundWork()
{
QMutexLocker lock(m_myMutex);
bool waitStatus = false;
QFuture myBackgroundTask = QtConcurrentRun::run(&MyClas::threadProc, &m_waitCondition);
if(myBackgroundTask.isStared())
{
waitStatus = m_waitCondition.wait(&m_myMutex, 200);
}
if(waitStatus)
{
/// Success
}
else
{
/// Timeout
}
}
bool MyClass::threadProc(QWaitCondition* pwaitCondition)
{
QMutexLocker locker(m_myMutex);
/// Do stuff here
pwaitCondition->wakeAll();
}