1

Are QMutex+QWaitCondition objects reusable? I have a method to wait for some threads to complete.

void FinishWait()
{
    mutex.lock();
    waitCondition(&qMutex, ULONG_MAX);
}

The waitCondition.wakeOne() is called on a different Thread.

The first call to FinishWait() works but the the second call doesn't return even if I specify timeout.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
tvr
  • 4,455
  • 9
  • 24
  • 29

1 Answers1

4

Assuming "mutex" and "qMutex" mean the same variable (it doesn't make much sense otherwise), you need to unlock the mutex after the wait() returns.

void FinishWait()
{
    mutex.lock();
    waitCondition.wait(&mutex);
    mutex.unlock();
}

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • The documentation http://doc.qt.nokia.com/latest/qwaitcondition.html#wait says: "The mutex will be unlocked, and the calling thread will block until either of these conditions is met...." – tvr Mar 07 '11 at 10:57
  • 3
    And if you read further I am pretty sure the documentation says something like "If the wait() function exits, the mutex is atomically locked again", because thats the semantics of a wait condition. – Gunther Piez Mar 07 '11 at 11:00