3 ms·
I read the section in that book about storing each thread's count in a TLS variable, and I think the root of the confusion is related to how C vs. Rust (the lan
by jstrong 6y ago
I read the section in that book about storing each thread's count in a TLS variable, and I think the root of the confusion is related to how C vs. Rust (the language I use and learned how to write multithreaded code in) deal with threads.
if I wanted to store a per-thread count in rust, it would make zero sense to use TLS for that, it would just be on the stack in the context of the thread's lexical scope:
let mut threads = Vec::new();
let global_count = Arc::new(AtomicUsize::new(0));
for _ in 0..n_threads {
threads.push(std::thread::spawn({
let global_count = global_count.clone();
move || {
let mut thread_count = 0;
for _ in some_iteration {
thread_count += 1;
}
global_count.fetch_add(thread_count, Ordering::Release);
}
});
}
for handle in threads { let _ = handle.join().unwrap(); }
println!("global count = {}", global_count.load(Ordering::Acquire));
that is a form of "thread-local storage", I guess, but does not involve any of the TLS primitives.