Locoduino / RingBuffer

A RingBuffer library for Arduino
GNU General Public License v2.0
63 stars 18 forks source link

how can I access to a ringBuffer as a pointer #17

Open buriansandor opened 3 months ago

buriansandor commented 3 months ago

Hi, I would like to access this RingBuffer as a pointer. Does this solution have such an option?

Koryphon commented 3 months ago

Hello. You mean you want to take a pointer on a ring buffer object ?

best regard

buriansandor commented 3 months ago

My idea is that I want to get it as a function parameter as a pointer. How can I do this?

Koryphon commented 3 months ago

Do it as you do for any other data type but I suggest you pass it as reference instead of pointer.

Koryphon commented 3 months ago

A full example with both reference and pointer RingBufAsRefPtr.zip

buriansandor commented 2 months ago

Ok, thank you! Now I understood. I want to access it from two different threads on ESP32, this is why I think it is better as a pointer.

Koryphon commented 2 months ago

Hello.

You can access it on different threads (I suppose you mean FreeRTOS tasks) as variable. Using a pointer will not make any difference because in the end the race condition will occur in the buffer storage, not in the way the code accesses the storage.

However, if you use you have to use a semaphore to prevent preemption. A binary semaphore is ok. Your code hase to take it before calling any function of the ring buffer and release it after.