Skip to content

Commit 2ed6865

Browse files
authored
Merge pull request #92 from polarathene/docs/use-thread-scope-example
docs: Switch to `thread::scope()` for example
2 parents fdf06db + 570bad3 commit 2ed6865

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,24 @@
4040
//!
4141
//! ```rust
4242
//! use thread_local::ThreadLocal;
43-
//! use std::sync::Arc;
4443
//! use std::cell::Cell;
4544
//! use std::thread;
4645
//!
47-
//! let tls = Arc::new(ThreadLocal::new());
46+
//! let tls = ThreadLocal::new();
4847
//!
4948
//! // Create a bunch of threads to do stuff
50-
//! for _ in 0..5 {
51-
//! let tls2 = tls.clone();
52-
//! thread::spawn(move || {
53-
//! // Increment a counter to count some event...
54-
//! let cell = tls2.get_or(|| Cell::new(0));
55-
//! cell.set(cell.get() + 1);
56-
//! }).join().unwrap();
57-
//! }
49+
//! thread::scope(|scope| {
50+
//! for _ in 0..5 {
51+
//! scope.spawn(|| {
52+
//! // Increment a counter to count some event...
53+
//! let cell = tls.get_or(|| Cell::new(0));
54+
//! cell.set(cell.get() + 1);
55+
//! });
56+
//! }
57+
//! });
5858
//!
5959
//! // Once all threads are done, collect the counter values and return the
6060
//! // sum of all thread-local counter values.
61-
//! let tls = Arc::try_unwrap(tls).unwrap();
6261
//! let total = tls.into_iter().fold(0, |x, y| x + y.get());
6362
//! assert_eq!(total, 5);
6463
//! ```

0 commit comments

Comments
 (0)