Expand description
AtomicBox and AtomicOptionBox are safe, owning versions of std’s AtomicPtr.
This can be useful to avoid resource leaks when you forget to call Box::from_raw on
the pointer stored in AtomicPtr.
Unfortunately, the only operations you can perform on an atomic box are swaps and stores: you
can not just use the box without taking ownership of it. Imagine a Box without Deref or
DerefMut implementations, and you can get the idea. Still, this is sufficient for some
lock-free data structures, so here it is.
§Why no Deref?
It would not 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 not 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.
Structs§
- Atomic
Box - A type that holds a single
Box<T>value and can be safely shared between threads. - Atomic
Option Box - A type that holds a single
Option<Box<T>>value and can be safely shared between threads.