You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::cell::Cell;
use std::sync::Arc;
use std::thread;
use thread_local::ThreadLocal;
fn main() {
let tls = Arc::new(ThreadLocal::new());
for _ in 0..5 {
let tls2 = tls.clone();
thread::spawn(move || {
let cell = tls2.get_or(|| Cell::new(0));
cell.set(1);
})
.join()
.unwrap();
}
let tls = Arc::try_unwrap(tls).unwrap();
let mut all_prams = vec![];
tls.into_iter().for_each(|y| all_prams.push(y.get()));
assert_eq!(all_prams, vec![1, 1, 1, 1, 1]);
}
Report the follow error:
all_prams: [1]
thread 'main' panicked at src/main.rs:21:5:
assertion `left == right` failed
left: [1]
right: [1, 1, 1, 1, 1]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Isn't the threadlocal object reporting an error with the content in each thread? Why is this happening?
The text was updated successfully, but these errors were encountered:
You can use a HashMap<ThreadId, T> in each thread-local variable and then merge the maps at the end. This will still be faster than using a Mutex<HashMap>.
You can use a HashMap<ThreadId, T> in each thread-local variable and then merge the maps at the end. This will still be faster than using a Mutex<HashMap>.
I run the following code:
Report the follow error:
Isn't the threadlocal object reporting an error with the content in each thread? Why is this happening?
The text was updated successfully, but these errors were encountered: