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.

Qt Code:
  1. QMutex m_locker;
  2.  
  3. // This method is executed in main thread
  4. int MyClass::Start()
  5. {
  6. do
  7. {
  8. bool bNoWork = true;
  9.  
  10. // find work
  11. // execute work
  12.  
  13. if (bNoWork)
  14. {
  15. m_locker.lock();
  16. m_wait.wait(&m_locker);
  17. m_locker.unlock();
  18. }
  19. } while (true);
  20. return 0;
  21. }
  22.  
  23.  
  24. // This method is always called from a worker thread
  25. void MyClass::OnChange()
  26. {
  27. m_wait.wakeOne();
  28. }
To copy to clipboard, switch view to plain text mode