I need to pass pointers to structures between threads. No networking nor radio communication is involved.
I picked the IPC messaging feature, which looks cool. But now, I’m concerned with concurrency and I’m wondering if I wouldn’t better be off with standard functions using mutex locks. I’m not sure if mutexes play well with messaging.
Can someone enlighten me?
If you use core_msg you will find that it also uses a mutex under the hood.
Be aware though that messages are stored in the receiving thread’s message queue, if the queue is full the message will be lost. (You of course also have to keep the life time of the data structure where you pointer points to in mind, e.g. it shouldn’t be allocated on the stack, otherwise your pointer points to something random once it goes out of scope)
I set up asynchronous IPC with a message queue.
Here is the case I had in mind:
- Thread A sends a message (
msg_try_send()), whose payload is a pointer to a struct, to thread B.
- Thread B processes the data pointed to by this pointer.
- Thread A updates the data as thread B is in the middle of the processing.
In this case, what are the options to protect the data? Mutex? Circular buffer (to avoid updating the data in-place) ?