QSemaphore for 2 QThreads to Control 2 Real-time Plots
-
Hello, I had a very confusing issue. I wrote a QT program using 2 threads.
- Main thread: GUI thread, and draw 2 real-time plots using 1
QCustomPlot
widget. Their x axis are synchronized. - Data thread: continuely fetch real-time data and process data. The raw data and the processed data would be drawn on 2 different real-time plots. I used a buffer to store data. After recieving some number of data points, it would emit functions to plot.
Here is the problem: The data thread can emit drawing action to 2 different plots in the main thread independently. I used
QSemaphore
to control the drawing part.
Global variables:QSemaphore freeBytes(1); QSemaphore usedBytes;
In data thread:
freeBytes.acquire(); emit updateChart1(data); usedBytes.release();
In main thread:
usedBytes.acquire(); _chart->updateAndDraw1(data); freeBytes.release();
This works pretty well if I only draw 1 plot, and I could draw real-time plot.
However, I don't know I could draw 2 plots. Let's say we have two functionsupdateChart1(data)
andupdateChart2(data)
to update 2 plots recpectively. These two functions can be emitted at any time independently. They don't always plot at the same time, thoughupdateChart1(data)
andupdateChart2(data)
could be running at the same time. If I usedQSemaphore
, the thread would be blocked, and the second plot would be drawn with latency.
How would I draw this 2 real-time plots in this situation? Am I usingQSemaphore
wrong? Or are there any better ways to achieve this goal?
If there's anything unclear in my description, please tell me. Any help is highly appreciated! - Main thread: GUI thread, and draw 2 real-time plots using 1