Next: , Previous: Usage, Up: Top


4 Code

This section of the manual is only for those interested into learning how kacq works at the code level. That might be usefull if you want to modify it, fix a bug or improve it.

The code behind kacq is relatively simple. When kacq is recording data, 4 different threads are running. The main thread of the prograim respond to the user input, and manages 3 other processing threads. The main advantage of the multithreaded programs is that they can take advantage of multi-core processors. Given that the interactions between the 3 processing threads are limited at exchanging a buffer of raw data, many operations can occurs in parallel in the different threads without problem. One of the advantage is that heavy processing of the data in the displaying thread is unlikely to block the acquisition thread and cause buffer overflow. Ideally, poor performance in the displaying thread should only impairs the display and not interfere with acquisition and recording of the data.

4.1 Acquisition thread

This thread is responsible to fetch the data from the AD devices and to make the data available to the rest of the program. There are two main structures representing this stage. One represents each individual device that are installed on the computer. The other is a comedi_interface which controls and syncrhonyzes the individual devices. The acquired data are available in the data_buffer of the comedi_interface structure. To avoid inconsistencies due to operations by multiple threads upon the same memory area performed at the same time or to prevent race conditions, this is the only memory chunck that is shared between the three processing threads. Moreover, when one thread read or write in that data_buffer, the other threads are prevented from reading or writing in it and must wait.

4.2 Recording thread

The recording thread gets the acquired data from the comedi_interface and copies the data needed into a recording_buffer. When enough data have accumulated into the recording_buffer, a call to the API write() is performed to save the data on a hard drive. Only the channels selected in the preference dialog box will be saved.

4.3 Displaying thread