Module alloc::arc
[−]
[src]
Threadsafe reference-counted boxes (the Arc<T>`Arc
The Arc<T>`Arctype provides shared ownership of an immutable value. Destruction is deterministic, and will occur as soon as the last owner is gone. It is marked asSend` because it uses atomic reference counting.
If you do not need thread-safety, and just need shared ownership, consider
the Rc<T>`RcArc<T>`Arc
The downgrade method can be used to create a non-owning Weak<T>`Weakpointer to the box. AWeakpointer can be upgraded to anArcpointer, but will returnNone` if the value has already been dropped.
For example, a tree with parent pointers can be represented by putting the
nodes behind strong Arc<T>`Arcpointers, and then storing the parent pointers asWeak
Examples
Sharing some immutable data between threads:
use std::sync::Arc; use std::thread; let five = Arc::new(5); for _ in 0..10 { let five = five.clone(); thread::spawn(move || { println!("{:?}", five); }); }
Sharing mutable data safely between threads with a Mutex`Mutex`:
use std::sync::{Arc, Mutex}; use std::thread; let five = Arc::new(Mutex::new(5)); for _ in 0..10 { let five = five.clone(); thread::spawn(move || { let mut number = five.lock().unwrap(); *number += 1; println!("{}", *number); // prints 6 }); }
Structs
| Arc |
An atomically reference counted wrapper for shared state. |
| Weak |
[Unstable] A weak pointer to an |
Functions
| get_mut | [Deprecated] |
| strong_count |
[Deprecated] Get the number of strong references to this value. |
| weak_count |
[Deprecated] Get the number of weak references to this value. |