Use multi-thread implementation data real-time collection

xiaoxiao2021-03-06  78

I am doing industry software, so I often come into contact with real-time acquisition of data. The general problem is that one thread reads data, and other threads processes the data (such as drawing real-time curves, depositing data into files, etc.). This involves two aspects. 1, synchronous problems of thread - typical producers and consumers; 2, data structures. The data intended is stored and read by consumers.

Below I will explore these two questions and everyone. I believe that everyone is not unfamiliar with the manufacturer's consumers. In reading, we use the P, V solution provided by the system body, which is a protective measures for reading and writing in the same critical area, and this project uses a buffer queue, so it is not necessary to lock the critical region. I will realize the dual cached version. I will implement an addition or or lower lock in the critical regions in this release.

The read data is stored in the corresponding data structure, and there are many options, such as the task buffer queue (by a chain table implementation), most of these engineering usage loop queues are used to write more read, write threads write data Queue, read threads read from the queue. Or implement a dual cache method, that is, the write thread is filled with a cache read thread to take the data simultaneously write the data to another cache.

The specific implementation is as follows:

The node of the list is declared as follows:

Typedef struct node

{

Int buffer [max_buffer_size];

Long counters;

Bool readenable;

Node * pnext;

} Node;

Where array buffer [] is used to store acquisition data. Counters is used to describe how many read threads have access to nodes. If all read threads have accessed this node, you can delete this node! Readenable indicates whether its node is readable.

The declaration of the list is as follows:

Typedef struct list

{

Node * predptr;

Node * PWRITEPTR;

Node * Pheadptr;

List;

Where PREADPTR is a pointer to the readable node, PWRitePtr is a pointer to the writable node. The preadptr is behind PWRITEPTR, that is, write pointers cannot exceed the read pointer. PHEADPTR points to the pointer to the first node in the list.

I use a write thread two read threads, or you can implement more than two readings. Friends can try only the counts in the list.

DWORD WINAPI Readerone (Void * pvoid);

DWORD WINAPI ReadertWO (Void * pvoid);

DWORD WINAPI WRITERUNIQUE (Void * pvoid);

In Writerunique I use dynamic allocation nodes, simultaneous analog data acquisition fill the Buffer array of Node. Of course, you should lock the write thread. Access to the nodes in the list in Readerone and ReadertWo and appear in the dialog box.

Note the question:

1. Because the node is dynamically allocated, so after New, it must be delete, otherwise, the memory will be slowly eaten by the program.

2, after the lock, must unlock otherwise it will cause a deadlock problem.

3, first click "Start Write Thread" and click the "Start Reading" program to click "End Write Thread" before exiting.

Interested friends can ask for source code! My Mail is savage54321@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-120565.html

New Post(0)