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

Is this library thread safe? #7

Open
skyformat99 opened this issue Jun 3, 2018 · 5 comments
Open

Is this library thread safe? #7

skyformat99 opened this issue Jun 3, 2018 · 5 comments

Comments

@skyformat99
Copy link

Can it be used in a multi-producer and multi-consumer thread environment?

@dhess
Copy link
Owner

dhess commented Jun 3, 2018

It may work, depending on your compiler and your CPU architecture, but I would not depend on it. The only way to do this portably and safely would be to add pthread support. I have avoided that to date because the primary goal of this project was to provide a portable, bug-free ring buffer implementation, rather than a particularly fast one.

@dhess
Copy link
Owner

dhess commented Jun 3, 2018

(Apparently C11 may make this possible as well? See #4)

@j-omega
Copy link

j-omega commented May 16, 2019

I am using this in a multi-threaded environment. Line 282 in ringbuf.c needs to be removed to work correctly:

assert(count + ringbuf_bytes_used(src) == bytes_used);

This will fail if an item is added to the ring buffer in the middle of the ringbuf_memcpy_from() call.

@dhess
Copy link
Owner

dhess commented May 16, 2019

There are many more changes than that required to guarantee that this implementation will work in a multi-threaded program, I’m afraid. It may happen to work for you on a particular CPU when compiled with a particular compiler, but it’s not something you should depend on.

As an aside, you can disable assert() statements in C by defining the NDEBUG macro at compile time. See https://en.cppreference.com/w/c/error/assert

@JensGrabner
Copy link

I use these: https://github.com/wizard97/Embedded_RingBuf_CPP -- it works fine.

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

4 participants