JuliaCollections / DataStructures.jl

Julia implementation of Data structures
https://juliacollections.github.io/DataStructures.jl/latest/
MIT License
679 stars 242 forks source link

Question regarding CircularBuffer #868

Closed gitboy16 closed 10 months ago

gitboy16 commented 10 months ago

Hi, Is the implementation of CircularBuffer lock free? Thank you Regards.

Ellipse0934 commented 10 months ago

There is no lock however it's not thread safe to begin with.

cmcaine commented 10 months ago

No, none of the data structures in this repo are suitable for concurrent use. You want ConcurrentCollections.jl.

To help you work this out for yourself in the future, here are some hints:

Firstly, any kind of concurrent data structure will be significantly slower than a serial one, so unless a data structure says that it is lock-free then it usually won't be.

Secondly, lock-free algorithms avoid locks by using atomic operations (compare-and-swap, etc). So you can just check the source and see if any atomic operations are being used. Atomic operations in Julia use Threads.Atomic or Base.@atomic (manual), so we can just search the project for the string "atomic". Look, no hits. None of the data structures in this repo are lock-free.

If you don't trust github search, you could check one of the methods for the data structure, e.g. pop!. It would need to have some kind of atomic operation if the data structure were lock free, and it doesn't.

gitboy16 commented 10 months ago

Thank you both.