Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
buriansandor opened this issue Jul 29, 2024 · 6 comments
Open

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

buriansandor opened this issue Jul 29, 2024 · 6 comments
Assignees

Comments

@buriansandor
Copy link

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

@Koryphon
Copy link
Member

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

best regard

@buriansandor
Copy link
Author

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

@Koryphon
Copy link
Member

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
Copy link
Member

A full example with both reference and pointer
RingBufAsRefPtr.zip

@Koryphon Koryphon self-assigned this Aug 1, 2024
@buriansandor
Copy link
Author

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
Copy link
Member

Koryphon commented Aug 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants