File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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//! ```
You can’t perform that action at this time.
0 commit comments