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 as
` type provides shared ownership of an immutable value.
Destruction is deterministic, and will occur as soon as the last owner is
gone. It is marked as Send
`Send` 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
`downgrademethod can be used to create a non-owning
` method can be used to create a non-owning Weak<T>
`Weakpointer to the box. A
` pointer
to the box. A Weak<T>
`Weakpointer can be upgraded to an
` pointer can be upgraded to an Arc<T>
`Arcpointer, but will return
` pointer, but
will return None
`None` 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 as
` pointers, and then storing the parent pointers
as Weak<T>
`Weak
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 |
[Unstable] Returns a mutable reference to the contained value if the |
strong_count |
[Unstable] Get the number of strong references to this value. |
weak_count |
[Unstable] Get the number of weak references to this value. |