Hi, my current situation is this:

I'm using threads in my application and i want to communicate with the main thread.
At the moment I'm doing it like this:

thread:
Qt Code:
  1. [...]
  2. extern QMutex waitForUserLock;
  3. extern QWaitCondition waitForUser;
  4. [...]
  5. text = [...];
  6. customEvent * send_event = new customEvent(type);
  7. send_event->setData((void*)text);
  8.  
  9. waitForUserLock.lock();
  10. QApplication::postEvent((QObject*)mainwindow, send_event);
  11. waitForUser.wait(&waitForUserLock); //sometimes hangs here
  12. waitForUserLock.unlock();
  13. [...]
To copy to clipboard, switch view to plain text mode 

mainwindow:
Qt Code:
  1. [...]
  2. QMutex waitForUserLock;
  3. QWaitCondition waitForUser;
  4. [...]
  5. void mainwindow::customEvent(QEvent *e)
  6. {
  7. customEvent *me = (customEvent *) e;
  8. switch (me->type())
  9. {
  10. case type:
  11. waitForUserLock.lock();
  12. my_function((char*)me->data());
  13. waitForUser.wakeOne();
  14. waitForUserLock.unlock();
  15. return;
  16. [...]
  17. }
  18. }
  19. [...]
To copy to clipboard, switch view to plain text mode 

The problem is the wait, where the application sometimes hangs.
Does anyone know where the mistake is or is there a better solution?