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:
[...]
extern QMutex waitForUserLock;
[...]
text = [...];
customEvent * send_event = new customEvent(type);
send_event->setData((void*)text);
waitForUserLock.lock();
waitForUser.wait(&waitForUserLock); //sometimes hangs here
waitForUserLock.unlock();
[...]
[...]
extern QMutex waitForUserLock;
extern QWaitCondition waitForUser;
[...]
text = [...];
customEvent * send_event = new customEvent(type);
send_event->setData((void*)text);
waitForUserLock.lock();
QApplication::postEvent((QObject*)mainwindow, send_event);
waitForUser.wait(&waitForUserLock); //sometimes hangs here
waitForUserLock.unlock();
[...]
To copy to clipboard, switch view to plain text mode
mainwindow:
[...]
[...]
void mainwindow
::customEvent(QEvent *e
) {
customEvent *me = (customEvent *) e;
switch (me->type())
{
case type:
waitForUserLock.lock();
my_function((char*)me->data());
waitForUser.wakeOne();
waitForUserLock.unlock();
return;
[...]
}
}
[...]
[...]
QMutex waitForUserLock;
QWaitCondition waitForUser;
[...]
void mainwindow::customEvent(QEvent *e)
{
customEvent *me = (customEvent *) e;
switch (me->type())
{
case type:
waitForUserLock.lock();
my_function((char*)me->data());
waitForUser.wakeOne();
waitForUserLock.unlock();
return;
[...]
}
}
[...]
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?
Bookmarks