xQueueSend with struct pointer and mutexing

Hi all,

I’m looking to mimic the very nicely demonstrated xQueueSend() implementation from the documentation.

I’m hoping to clarify my understanding of the above linked demonstration, for the use case of sending a struct pointer to the queue. If the ‘sending’ task is modifying the values of the struct at a relatively high frequency, what is the standard/most performant way to ensure that the struct values aren’t changed between the struct pointer being sent to the queue, and the ‘receiving’ task processing the struct values?

If my understanding is correct, the address of the struct is copied to the queue, so there’s nothing stopping the struct from being altered between sending and receipt/processing of the struct’s address.

I tend to use task notifications wherever possible, but would this be a situation where a binary semaphore would have to be used? If so, consideration would need to be made to ensure the ‘receiving’ task processes the queue as quickly as possible, to prevent any lengthy blocking of the ‘sending’ task from updating the struct?

Any clarification would be greatly appreciated!

Thanks!

When I send struct pointers, I tend to use some sort of pool of the structs, and the task filling in the struct gets a pointer to a free struct from the pool, fills it in, and send it, and then forgets that pointer, and either gets a new one from the pool, or just nulls out its pointer till it needs a new one.

The receiving task uses the pointer, and gets what it needs out of it, and then returns to struct pointer back into the pool.

The other option is to have a semaphore/task notification that the receive sets when it is done.with the structure, and the sender will wait on before it updates it after sending.

1 Like

Thank you as always Richard, your input is greatly appreciated.

I quite like the idea of establishing a fixed/known array of structs, and cycling through them in each iteration of the sending task, with the pointer to the current struct being added to the queue. I’ll have a play with that concept and see how I go.

Quick follow up question. Would the approach be the same, if instead of having a pool of structs and sending a pointer to a struct, you had a pool of arrays (e.g. uint8_t someArray[10]), and you sent a pointer to an array to the queue?

If so, how would the corresponding ‘receiving’ task be setup to receive the pointer?

The method described by Richard remains the same. It doesn’t matter if it’s a pointer to a struct or an array.

The only difference between a struct and an array here would be how you declare the pointer, and the fact that depending on what you are doing with the pointer, if you are sending a pointer to an array of uint8_t, you might just want to get back a pointer to a uint8_t instead of an array of uint8_t.