vorner / arc-swap

Support atomic operations on Arc itself

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`ArcSwap<T>` vs crossbeam `AtomicCell<Arc<T>>`

shikhar opened this issue · comments

Thank you for this neat library, exactly what I needed! After I started using it, I came across crossbeam's AtomicCell and I was curious to get your thoughts on how they compare.

Compare in what regard, what do you want to know?

ArcSwap is specifically designed to work in lock-free manner with Arcs, including making more copies of them (atomically). That's the tricky part (and the useful part), but it also specializes to that.

AtomicCell seems to work with many things, not just Arcs, but has many limitations on what you can do with it ‒ similar to the ones of ordinary Cell. For example, the load method is only available for Copy types ‒ which Arc isn't, therefore AtomicCell<Arc<T>> is kind of degraded thing ‒ it can swap, but can't load.

   --> src/main.rs:9:24
    |
9   |     println!("{:?}", x.load());
    |                        ^^^^ method cannot be called on `AtomicCell<Arc<usize>>` due to unsatisfied trait bounds
    |
   ::: /mnt/cache/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/sync.rs:235:1
    |
235 | pub struct Arc<T: ?Sized> {
    | ------------------------- doesn't satisfy `Arc<usize>: Copy`
    |
    = note: the following trait bounds were not satisfied:
            `Arc<usize>: Copy`

I'd say you'd use AtomicUsize for numbers, AtomicCell<MyEnum> for something very primitive (in terms of memory representation/behaviour) and ArcSwap for Arcs that need their reference counts updated atomically.

(Closing this, as this doesn't seem to be any „actionable“ issue).

Thanks, I didn't know where else to get your take. I appreciate it, it is good info if anyone has the same question.

AtomicCell<Arc<T>> is kind of degraded thing ‒ it can swap, but can't load.

I didn't realize (should have tried it out before asking!). That settles it :)