Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSemaphore for 2 QThreads to Control 2 Real-time Plots
Forum Update on Monday, May 27th 2025

QSemaphore for 2 QThreads to Control 2 Real-time Plots

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthreadreal time plot
1 Posts 1 Posters 273 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pandako
    wrote on 25 Jul 2022, 08:26 last edited by
    #1

    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 functions updateChart1(data) and updateChart2(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, though updateChart1(data) and updateChart2(data) could be running at the same time. If I used QSemaphore, 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 using QSemaphore 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!

    1 Reply Last reply
    0

    1/1

    25 Jul 2022, 08:26

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved