I have a Qt GUI application which does some I/O bound work when a button is pressed. In order to avoid GUI not being responsive, I created a new thread and move the work there:
private slots:
inline void on_process_button_clicked() const
{
std::thread thread(&My_class::on_process_button_clicked_real_work, this);
thread.detach();
}
I detach the thread immediately. The other function simply does the real work:
void on_process_button_clicked_real_work() const
{
std::lock_guard<std::mutex> lock(mutex);
// Some irrelevant code ...
}
The GUI now doesn't entirely freeze, I can still see it updated, but it becomes really unresponsive and laggy.
Questions:
1. Why does this happen?
2. How may I fix it?
I have seen many similar question, but most are about QThread
so I couldn't solve my problem.