4 releases (breaking)

0.4.0 Nov 30, 2021
0.3.0 Nov 18, 2019
0.2.0 Feb 8, 2019
0.1.0 Feb 8, 2019

#323 in Concurrency

Download history 132/week @ 2024-07-19 380/week @ 2024-07-26 727/week @ 2024-08-02 499/week @ 2024-08-09 381/week @ 2024-08-16 506/week @ 2024-08-23 325/week @ 2024-08-30 487/week @ 2024-09-06 358/week @ 2024-09-13 502/week @ 2024-09-20 461/week @ 2024-09-27 660/week @ 2024-10-04 386/week @ 2024-10-11 338/week @ 2024-10-18 397/week @ 2024-10-25 177/week @ 2024-11-01

1,373 downloads per month
Used in 3 crates

MIT/Apache

28KB
383 lines

atomicbox

Safe atomic boxes.

Documentation Build Status

This crate provides AtomicBox<T> and AtomicOptionBox<T> types: safe, owning versions of the standard library's AtomicPtr.

Unfortunately, the only operations you can perform on an atomic box are swaps and stores: you can't just use the box without taking ownership of it. Imagine a Box without Deref or DerefMut implementations, and you'll get the idea. Still, this is sufficient for some lock-free data structures, so here it is!

Why no Deref?

It wouldn't be safe. The point of an AtomicBox is that other threads can obtain the boxed value, take ownership of it, even drop it, all without taking a lock. So there is no safe way to borrow that value—except to swap it out of the AtomicBox yourself.

This is pretty much the same reason you can't borrow a reference to the contents of any other atomic type. It would invite data races. The only difference here is that those contents happen to be on the heap.

License: MIT/Apache-2.0

No runtime deps