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

#423 in Concurrency

Download history 58/week @ 2024-02-25 20/week @ 2024-03-03 21/week @ 2024-03-10 19/week @ 2024-03-17 63/week @ 2024-03-24 46/week @ 2024-03-31 39/week @ 2024-04-07 80/week @ 2024-04-14 173/week @ 2024-04-21 157/week @ 2024-04-28 291/week @ 2024-05-05 287/week @ 2024-05-12 187/week @ 2024-05-19 112/week @ 2024-05-26 336/week @ 2024-06-02 175/week @ 2024-06-09

821 downloads per month
Used in 6 crates (3 directly)

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