Hey All,

I am a newbie in C programming. I have written a program which has a separate function to send messages to 2 neighbours in the same network. This function is called from the main program. Also I have a thread which is created by the main function. The purpose of the thread is to listen for incoming messages. So irrespective of what happens in the main function, the function which is executed via the thread should keep receiving messages(whenever one is received) and should store it in a queue. Once in every machine cycle, I want to check the queue and process the received messages.

Earlier I had both the receive and the send module in the main function, which eventually ended up in a deadlock situation. So to get rid of the interdependencies between the send and receive module, I thought of using the thread concept. Now I think the thread would execute the receive function independently and once in every machine cycle I can read the messages and process it through another function.

My question is this. Is there a inbuilt option in C programming to store the incoming messages in a queue and later the main function can process it? I know there is something called as message queue in C programming. But my understanding with "message queues" is something like sharing the messages within two processes using a message header. That would have its pre-defined format to store the message I guess. I have my own format for the incoming messages (I will be receiving a structure that will have 3 arrays and a char variable). Is there a way that I can store this structure along with the ip address of the sender in a common buffer or a queue for later retrieval?? I can store it in a buffer I know. But that involves so much of coding. If there is something inbuilt and simple in C programming, that would greatly help me.. Please help.

Thanks in Advance,

7 Years
Discussion Span
Last Post by BestJewSinceJC

Considering that you just said you will be receiving a struct, as long as you know the format of the struct, you can use an array of structs to handle your message queue. This link describes how to create/use arrays of structs.


As for the threading question, you would need to share the array of structs between your two threads. Since I've never programmed multi-threaded programs in C, I don't know how it would work exactly (without global variables), but I'm sure someone else will be able to explain that to you.

Edited by BestJewSinceJC: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.