Struct sync::Arc
pub struct Arc<T> { // some fields omitted }
An atomically reference counted wrapper for shared state.
Example
In this example, a large vector of floats is shared between several tasks.
With simple pipes, without Arc
, a copy would have to be made for each
task.
use sync::Arc; fn main() { let numbers = Vec::from_fn(100, |i| i as f32); let shared_numbers = Arc::new(numbers); for _ in range(0, 10) { let child_numbers = shared_numbers.clone(); spawn(proc() { let local_numbers = child_numbers.as_slice(); // Work with the local numbers }); } }
Methods
impl<T: Share + Send> Arc<T>
fn new(data: T) -> Arc<T>
Create an atomically reference counted wrapper.
fn downgrade(&self) -> Weak<T>
Downgrades a strong pointer to a weak pointer
Weak pointers will not keep the data alive. Once all strong references to the underlying data have been dropped, the data itself will be destroyed.
impl<T: Send + Share + Clone> Arc<T>
fn make_unique<'a>(&'a mut self) -> &'a mut T
Acquires a mutable pointer to the inner contents by guaranteeing that the reference count is one (no sharing is possible).
This is also referred to as a copy-on-write operation because the inner data is cloned if the reference count is greater than one.
Trait Implementations
impl<T: Share + Send> Clone for Arc<T>
fn clone(&self) -> Arc<T>
Duplicate an atomically reference counted wrapper.
The resulting two Arc
objects will point to the same underlying data
object. However, one of the Arc
objects can be sent to another task,
allowing them to share the underlying data.