[Qt-interest] QWaitCondition wakeOne/wakeAll

Lars Amsel Lars.Amsel at signalion.com
Wed Jun 10 09:33:22 CEST 2009


Hi,

I have a question regarding QWaitCondition. The documentation states, that waiting for a condition ensure atomic transition from locked state to wait state.

What about the wakeOne(), wakeAll()? Is the transition from wait state to the locked state atomic too?

Somewhere in the code I have

 1:  mutex.lock();
 2:  waitCondition(&mutex);
 3:  processSomething();
 4:  msg = NULL;
 5:  mutex.unlock();

in another thread I do

 6:  waitCondition.wakeOne();
 7:  mutex.lock();
 8:  if (msg != NULL) {
 9:    doDefaultProcessing();
10:  }
11:  mutex.unlock();

Under rare conditions it occurs that msg is not NULL in line 8, especially when first and second call are close together. The only explanation I have is that the wakeOne() does not ensure that the mutex is locked after returning from that call.

How do I solve that? I thought about a second wait condition to wait for processSomething(). The problem there is, that the second code snippet could be executed without the first ever called. The wait for processSomething() would then wait eternally.

Cheers

Lars





More information about the Qt-interest-old mailing list