Struct std::sync::Arc
[−]
[src]
pub struct Arc<T> where T: ?Sized {
// some fields omitted
}An atomically reference counted wrapper for shared state.
Examples
In this example, a large vector of floats is shared between several threads.
With simple pipes, without Arc, a copy would have to be made for each
thread.
When you clone an Arc<T>, it will create another pointer to the data and
increase the reference counter.
use std::sync::Arc; use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).collect(); let shared_numbers = Arc::new(numbers); for _ in 0..10 { let child_numbers = shared_numbers.clone(); thread::spawn(move || { let local_numbers = &child_numbers[..]; // Work with the local numbers }); } }
Methods
impl<T> Arc<T>
fn new(data: T) -> Arc<T>
Constructs a new Arc<T>.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); }use std::sync::Arc; let five = Arc::new(5);
impl<T> Arc<T> where T: ?Sized
fn downgrade(&self) -> Weak<T>
: Weak pointers may not belong in this module.
Downgrades the Arc<T> to a Weak<T> reference.
Examples
#![feature(arc_weak)] fn main() { use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade(); }use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade();
fn weak_count(this: &Arc<T>) -> usize
Get the number of weak references to this value.
fn strong_count(this: &Arc<T>) -> usize
Get the number of strong references to this value.
impl<T> Arc<T> where T: Clone
unsafe fn make_unique(&mut self) -> &mut T
: this function is unsafe with weak pointers
Make a mutable reference from the given Arc<T>.
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.
This method is marked unsafe because it is racy if weak pointers are active.
Examples
#![feature(arc_unique)] fn main() { use std::sync::Arc; unsafe { let mut five = Arc::new(5); let mut_five = five.make_unique(); } }use std::sync::Arc; let mut five = Arc::new(5); let mut_five = five.make_unique();