4

I need to code a shared buffer (1R thread/1W thread) in C to asynchronously dump binary program output to I/O. I normally use the classical circular buffer implementation when it comes to write a simple shared buffer, but in this case the size of each item on the buffer is variable and not-known at compile time. I was wondering what is a possible implementation of a circular buffer with variable-sized items.

Thanks.

1 Answer 1

4

You may think of it as a circular binary stream. That is, instead if adding an element you'll write the data, instead of popping it you'll read it.

Sign up to request clarification or add additional context in comments.

2 Comments

how would you track the size to read ? writing it directly in the write index ?
you can write to the buffer a struct { int size_payload; char* payload}, after reading first int you know how much is the payload (variable-sized item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.