multithreading - Multithreaded c program design help -


I do not have much experience with multithreading and I am writing AC program which I believe is going to run in two threads Suitable for The program will talk to the serial port for data, it will read and process new data when available, and will publish the latest processed data to third party (via irrelevant) module on request (this misleading name is IPC) .

To receive requests for publishing data through IPC, the program must call the IPC_ListWit (wait_time); Then if the request to publish is received, a "listening" handler is applied to publish the latest data.

One option is to do this in a thread:

  for (;;) {read_serial (inputBuffer); Process_data (inputbuffer, processed data); // Process and store IPC_listenwait (wait_time); // If the request for publication is received during this time, // // a handler will be applied and // the latest part of the processed data will be published on other modules rueesthandler () {// When during IPC_listenwait If the message is received, then IPC_publish (newest processedData); }   

And this works, but it is important for the application that the program is very responsible for requesting to publish new data, and the published data is available latest. Are not satisfied with the above because the process starts after listening and the data can be received after requesting to publish the message. The process can be read / processing when the request to publish the message is coming, but it will not be able to serve the next IPC list list.

I can think that the only design is to read the thread, which will do just that: PreText () {for (;;) {// pseudocode Selection (); Read (inputBuffer); Process (inputbuffer, processed data); }}

The main thread to hear incoming messages is:

  mainThread () {IPC_listenwait (forever); } PublishRequestHandler () {// When IPC_listenwait receives the message during IPC_publish (latest (processed data)); }   

Will you use this design?

thanks

thanks

thanks

div class = "post-text" itemprop = "text">

Are on the right track.

One thing you have to look for is concurrent access to publishing data, because you do not want a thread to hold it while another is trying to read it. To prevent this, use a pair of buffers and a mute x-protected indicator, which is currently considered. When something is ready, its results should be dumped in non-current buffer, lock the pointer mute, assign the pointer with the new data in the buffer, and then release the mute x. Similarly, the publisher should lock the indicator mute, while he reads the current data, which may be compulsory for anyone who might want to stop it to wait. This is slightly more complicated than being a single, muteux-protected buffer, but will assure that you always have some data going on to be published, while new data is being prepared.

If your processing phase takes a long time, you can divide the reading / process thread into two to get multiple sets of data to read and the reader Make sure that the processor gets the latest and greatest ever, so you do not end up processing content that you will never publish. / P>

Long ago the first question, the way. Become an upshot.

Comments