I've a thread that read datas
class MyThread: QThread
{
...
}
void MyThread::run ()
{
uint8_t* buffer; // in my real code, it's a ring, so there is not read during write
// ...
while (true)
{
if (isInterruptionRequested())
return;
USB_READ(buffer);
emit newData(buffer);
}
}
In my UI Class I have:
connect(this, &UIClass::newData, m_thread, &MyThread::newData);
// ...
void newData(uint8_t* data)
{
// Process data
}
void UIClass::closeEvent(QCloseEvent *event)
{
disconnect(this, &UIClass::newData, m_thread, &MyThread::newData);
m_thread->requestInterruption();
m_thread->wait();
}
The problem with that if, when I click on "close", the thread is destroyed that cause the pointer data
to be invalid. The signal newData
is sometimes called that cause my function to work with invalid pointer and segfault. How to be sure that is not gonna happend ?
For now, I use a std::this_thread::sleep_for() with an arbitrary delay, it works, but I not find this very beautiful
That I have in my mind :
- disconnect the signal
- wait for the pendings signals to be executed
- exit