From 13913223621fa2e9c1d53eb59bd7e79f948b3e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justo=20Hern=C3=A1ndez=20Romera?= Date: Sat, 1 Aug 2020 18:26:40 +0200 Subject: [PATCH 1/2] Include Arc chapter. --- src/SUMMARY.md | 1 + src/std/arc.md | 27 +++++++++++++++++++++++++++ src/std/rc.md | 3 ++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/std/arc.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7e00336d6b..093d3af8ee 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -182,6 +182,7 @@ - [Alternate/custom key types](std/hash/alt_key_types.md) - [HashSet](std/hash/hashset.md) - [`Rc`](std/rc.md) + - [`Arc`](std/arc.md) - [Std misc](std_misc.md) - [Threads](std_misc/threads.md) diff --git a/src/std/arc.md b/src/std/arc.md new file mode 100644 index 0000000000..566bd55eca --- /dev/null +++ b/src/std/arc.md @@ -0,0 +1,27 @@ +# Arc + +When shared ownership between threads is needed, `Arc`(Atomic Reference Counted) can be used. This struct, via the `Clone` implementation can create a reference pointer for the location of a value in the memory heap while increasing the reference counter. As it shares ownership between threads, when the last reference pointer to a value is out of scope, the variable is dropped. + +```rust,editable + +fn main() { +use std::sync::Arc; +use std::thread; + +// This variable declaration is where it's value is specified. +let apple = Arc::new("the same apple"); + +for _ in 0..10 { + // Here there is no value specification as it is a pointer to a reference + // in the memory heap. + let apple = Arc::clone(&apple); + + thread::spawn(move || { + // As Arc was used, threads can be spawned using the value allocated + // in the Arc variable pointer's location. + println!("{:?}", apple); + }); +} +} + +``` diff --git a/src/std/rc.md b/src/std/rc.md index 86e00e9081..0c3aef4b2d 100644 --- a/src/std/rc.md +++ b/src/std/rc.md @@ -48,7 +48,8 @@ fn main() { ### See also: -[std::rc][1] and [Arc][2]. +[std::rc][1] and [std::sync::arc][2]. [1]: https://doc.rust-lang.org/std/rc/index.html + [2]: https://doc.rust-lang.org/std/sync/struct.Arc.html From 957687f1649eb80e17a8595a3cad3a46134bc431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justo=20Hern=C3=A1ndez=20Romera?= Date: Sat, 1 Aug 2020 18:28:52 +0200 Subject: [PATCH 2/2] Delete blank line in Rc chapter. --- src/std/rc.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/std/rc.md b/src/std/rc.md index 0c3aef4b2d..a9691b2448 100644 --- a/src/std/rc.md +++ b/src/std/rc.md @@ -51,5 +51,4 @@ fn main() { [std::rc][1] and [std::sync::arc][2]. [1]: https://doc.rust-lang.org/std/rc/index.html - [2]: https://doc.rust-lang.org/std/sync/struct.Arc.html