[libc-commits] [libc] [libc] Implement simple lock-free stack data structure (PR #83026)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Mon Feb 26 11:01:02 PST 2024


jhuber6 wrote:

> Given that the stack is of a fixed size (i.e. there cannot be too many different tags). Is ABA still a concern?

Theoretically you can `push(); pop();` over and over again, but I said it's unlikely.

> I'd suggest a structure more like
> 
> ```
> template <typename T, size_t N>
> struct stack
> {
>    T value[N]; 
>    bitmap<N> in_use;
> };
> ```

This works for the use-case of a fixed size buffer, but it is not immediately obvious how to get stack semantics from this, as you would need some extra information to indicate the ordering that would then require similar operations to the one in this implementation.

> The lottery analogy is not persuasive - the probability of this failure mode is far higher than a lottery and you can gamble a billion times a second instead of once a week.

If we assume that the thread being woken up after N stack operations is evenly distributed then this happening is 1 in 4294967295 if I'm not mistaken.

https://github.com/llvm/llvm-project/pull/83026


More information about the libc-commits mailing list