I have a program written in C and running on Linux which acquires streaming data from a serial port device every 16 or so ms. This is a time critical piece of code that works fine. Another piece of code plots this data, also in real time, but its timely execution is less important to me than the data acquisition part. That is, I don’t want to wait until all the plotting and drawing functions have finished before polling the serial port again. So I was thinking of having a separate thread do the plotting part of the application, or perhaps have the data acquisition part be the separate thread. I really have next to no experience when it comes to low-level programming, so could someone point me in the right direction? The pseudo-code with which I am working looks something like this:
int xyz; // global variable
int main() {
do_some_preliminary_stuff();
while 1 {
poll_serial_port_and_fill_xyz_with_new_position_and_repeat();
}
while 1 {
plot_xyz();
}
return 0;
}
Obviously as written, the code will be stuck in the first while loop, so yeah, threads?
Thanks.
Yup, that is the way to go. Have non-main thread be the data acquisition thread, which posts the buffered response to the main/UI thread which does the plotting. Main thread should consume this data for plotting.