I managed to implement a GUI Qt-based application in a worker C++ std::thread
like described here. Now I need both the main and worker threads to communicate.
My question is: how can I communicate messages (an array of floats) from my main thread to the worker thread so that I can update the GUI?
I have an application that performs real-time signal processing. My goal is to create a Qt GUI that can be plugged in into my application to visualize various signals without influencing the real-time aspect. I investigated different methods of how to accomplish this and concluded that this post describes quite closely what I need and provides a solution for it. However, there is no information on how the main and worker threads can communicate with each other.
I tried using the Futures/Promises approach described here to accomplish the inter-thread communication. Although I was able to get this example running, I couldn't integrate it into my project. The reason is that this approach relies on having a busy loop inside the worker thread that is constantly checking whether a new message has been sent by the main thread. However, in a Qt application, the program blocks once it enters the main event loop in a.exec()
. This prevents the busy loop check and hence causes the program to deadlock.
This is how I am spawning the GUI worker thread (based on this post).
#include <thread>
// Start the Qt realtime plot demo in a worker thread
int argc = 0;
char **argv = NULL;
std::thread t1
(
[&] {
QApplication application(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return application.exec();
}
);