Quantcast
Channel: Multithreaded c program design help - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Multithreaded c program design help

$
0
0

I don't have much experience with multithreading and I'm writing a c program which I believe is suited to running in two threads. The program will listen on the serial port for data, read and process new data when it's available, and publish the newest processed data to other (irrelevant) modules via a third party IPC api (it's confusingly named IPC) when requested.

In order to receive the request to publish data via IPC, the program must call IPC_listenwait(wait_time);. Then if a request to publish is received while "listenwaiting" a handler is invoked to publish the newest data.

One option is to do this in one thread like:

for(;;) {  read_serial(inputBuffer);  process_data(inputBuffer, processedData); //Process and store  IPC_listenwait(wait_time); //If a request to publish is received during this, }                            //then a handler will be invoked and the newest piece of                                     //processedData will be published to other modulespublishRequestHandler() { //Invoked when a message is received during IPC_listenwait  IPC_publish(newest(processedData));}

And this works, but for the application it is important that the program is very responsive to the request to publish new data, and that the data published is the newest available. These goals are not satisfied with the above because data may arrive after the process begins listenwaiting and before a request to publish message is received. Or the process may be reading/processing when a request to publish message is incoming, but won't be able to service it until the next IPC_listenwait call.

The only design I can think of is to have one thread to read, which will just do something like:

readThread() {    for(;;) { //pseudocode      select();      read(inputBuffer);      process(inputBuffer, processedData);    }}

And have the main thread just listening for incoming messages:

mainThread() {  IPC_listenwait(forever);}publishRequestHandler() { //Invoked when a message is received during IPC_listenwait  IPC_publish(newest(processedData));}

Is this the design you would use? If so, will I need to use a semaphore when accessing or writing processedData?

Will this give me good responsiveness?

Thanks


Viewing all articles
Browse latest Browse all 2

Trending Articles