This example demonstrates a real-time chart in which the data are acquired from a separate thread.
It is based on the Real-Time Chart with Zooming and Scrolling
code in the ChartDirector distribution, and is available in C++ (MFC, Qt) and C# (.NET Windows
The following only explains the multithreading part of the code. For how to create real-time
charts with zooming and scrolling interface, programmable track cursors, and save snapshots in
PDF/SVG/PNG/JPG/BMP, please refer to the documentation of the original Real-Time Chart with
Zooming and Scrolling sample code.
Real-Time Charts and Multithreading
Many real-time chart applications requires data to be acquired regularly in high speed. However,
the GUI timers are inaccurate as the GUI thread is affected by user interactions. For example,
on Windows, right clicking on the window title bar can cause the GUI message loop to halt until
the mouse button is released. For accurate timing, real-time data acquisition often needs to run
in a separate thread.
GUI components are constantly invoked by the operating system to handle system messages, such as
those for mouse and paint events. That means a GUI component should not be accessed from another
thread, such as the data acquisition thread, as this may result in uncoordinated concurrent access,
and can cause strange errors and random crashes.
In this example, data are acquired in a data acquisition thread and stored in a special
"double buffered queue". The GUI thread periodically reads data from the queue and updates the
real-time chart. This is illustrated below.
The "double buffered queue" is thread-safe and is designed so that reading and writing always work
on different buffers. This allows read and write to occur concurrently with minimal contention. The
functions of the queue are:
- With the queue, the data acquistion thread does not need to access any GUI component or wait for
the GUI thread to process the data. The prevents data acquisition from being affected by GUI events.
- The queue allows data acquisition and chart update to occur at different rates. For example,
the data acquistion thread can run thousands of times per second, while the GUI thread can run at a
slower rate and process data in large chunks.
*** Note *** :
If you are new to ChartDirector, it is
recommended you download ChartDirector from the download page
to try it.
It includes complete ChartDirector documentation as well as plenty of sample code designed as tutorials. The
following assumes you already have some basic understanding of ChartDirector.
The sample code in this page is included as part of the "Real-Time Chart Extra Sample Code Pack".
Real-Time Chart Extra Sample Code Pack for C++ (MFC, Qt)
- Remember to unblock the ZIP
file before extracting the contents out
- Includes Visual Studio solution (for C++/MFC), Qt project file (for C++/Qt), and both 32-bit and
64-bit ChartDirector Windows DLL.
- If you are using another operating system edition of ChartDirector, simply copy the sample code folder
inside the qtdemo folder to the qtdemo folder of your ChartDirector distribution, and you can compile and
run the Qt sample code.
The followings are links to the documentation of the original Real-Time Chart with Zooming and Scrolling sample code.