Skip to content

Commit

Permalink
Add ARC::get method and implements the function from it. Add an examp…
Browse files Browse the repository at this point in the history
…le showing a simple use of ARC.
  • Loading branch information
Olivier Saut committed May 20, 2013
1 parent a9c7d3f commit 3f232bc
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/libstd/arc.rs
Expand Up @@ -8,9 +8,33 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/**
/*!
* Concurrency-enabled mechanisms for sharing mutable and/or immutable state
* between tasks.
*
* # 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.
*
* ~~~
* extern mod std;
* use std::arc;
* let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random());
* let shared_numbers=arc::ARC(numbers);
*
* for 10.times {
* let (port, chan) = stream();
* chan.send(shared_numbers.clone());
*
* do spawn {
* let shared_numbers=port.recv();
* let local_numbers=shared_numbers.get();
*
* // Work with the local numbers
* }
* }
* ~~~
*/

use sync;
Expand Down Expand Up @@ -93,9 +117,14 @@ pub fn ARC<T:Const + Owned>(data: T) -> ARC<T> {
* wrapper.
*/
pub fn get<'a, T:Const + Owned>(rc: &'a ARC<T>) -> &'a T {
unsafe { &*rc.x.get_immut() }
rc.get()
}

impl<T:Const+Owned> ARC<T> {
pub fn get<'a>(&'a self) -> &'a T {
unsafe { &*self.x.get_immut() }
}
}
/**
* Duplicate an atomically reference counted wrapper.
*
Expand Down Expand Up @@ -508,6 +537,7 @@ mod tests {
c.send(arc::clone(&arc_v));

assert_eq!((*arc::get(&arc_v))[2], 3);
assert_eq!(arc_v.get()[4], 5);

info!(arc_v);
}
Expand Down

5 comments on commit 3f232bc

@bors
Copy link
Contributor

@bors bors commented on 3f232bc May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at osaut@3f232bc

@bors
Copy link
Contributor

@bors bors commented on 3f232bc May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging osaut/rust/arc-clean = 3f232bc into auto

@bors
Copy link
Contributor

@bors bors commented on 3f232bc May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osaut/rust/arc-clean = 3f232bc merged ok, testing candidate = 54eafc0

@bors
Copy link
Contributor

@bors bors commented on 3f232bc May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 3f232bc May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 54eafc0

Please sign in to comment.