RingBuffer icon indicating copy to clipboard operation
RingBuffer copied to clipboard

how can I access to a ringBuffer as a pointer

Open buriansandor opened this issue 1 year ago • 6 comments

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

buriansandor avatar Jul 29 '24 18:07 buriansandor

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

best regard

Koryphon avatar Jul 29 '24 18:07 Koryphon

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

buriansandor avatar Jul 31 '24 11:07 buriansandor

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

  • Declare the function with the correct type, for instance: void myFunc(RingBuf<byte, 20> &ioBuf);
  • Pass your data: myFunc(myBuf); assuming myBuf is declared as follow: RingBuf<byte, 20> myBuf;

Koryphon avatar Jul 31 '24 12:07 Koryphon

A full example with both reference and pointer RingBufAsRefPtr.zip

Koryphon avatar Jul 31 '24 12:07 Koryphon

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.

buriansandor avatar Aug 06 '24 08:08 buriansandor

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.

Koryphon avatar Aug 06 '24 08:08 Koryphon