creekorful creekorful - 2 months ago 10
C++ Question

How to unpause thread in Qt

I wanna know if there is a way to unpause a sleeping thread in Qt

I pause the thread using QThread::msleep(ms) function

And i want to unpause the thread before time runs out

Is there any way to do it ?

Thank in advance, Creekorful

Answer
QMutex mut;

void InterruptMethod() {
  mut.unlock();
}

void SleepMethod() {
  mut.lock();
  const int kTimeout = 5000;
  bool over_event_come = mut.tryLock(kTimeout); // here was sleep
  if (over_event_come) {
    // unlock from another place
  } else {
    // exit by timeout
  }
  mut.unlock();
}