PDA

View Full Version : QWaitCondition wakeone



redlars
6th September 2011, 14:23
The below is a skeleton of a class I am working on and I would appreciate some help.

The idea is that the Start() method will; find work, executed work and wait when there is no work. The OnChange() is supposed to notify the Start method whenever there is new work.

This design is not thread safe. If for instance the OnChange() is executed prior to m_wait.wait(&m_locker); then the Start() method will block until the next OnChange is called.

So basically the OnChange is a fire and forget with no persistent storage of the event.

How can I make OnChange() notify the Start() method is more persistent and thread safe way? Appreciate any input.



QMutex m_locker;
QWaitCondition m_wait;

// This method is executed in main thread
int MyClass::Start()
{
do
{
bool bNoWork = true;

// find work
// execute work

if (bNoWork)
{
m_locker.lock();
m_wait.wait(&m_locker);
m_locker.unlock();
}
} while (true);
return 0;
}


// This method is always called from a worker thread
void MyClass::OnChange()
{
m_wait.wakeOne();
}