Struct lockfree::tls::ThreadLocal
source · pub struct ThreadLocal<T> { /* private fields */ }
Expand description
Per Object Thread Local Storage. The stored data is not dropped on thread exit. It is only dropped when the structure itself is dropped. After the thread exited, the data might be reused for other threads. This TLS’s operation are also wait-free.
Example
extern crate lockfree;
use lockfree::tls::ThreadLocal;
use std::{cell::Cell, sync::Arc, thread};
let tls = Arc::new(ThreadLocal::<Cell<usize>>::new());
let mut threads = Vec::with_capacity(32);
for i in 1 ..= 32 {
let tls = tls.clone();
threads.push(thread::spawn(move || {
tls.with_default().set(i);
if tls.get().map(|c| assert_eq!(c.get(), i)).is_none() {
// Some OSes mis-run the destructors for their TLS
// implementation.
eprintln!("Warning: OS mis-reset the global thread state")
}
}))
}
for thread in threads {
thread.join().unwrap();
}
Implementations
sourceimpl<T> ThreadLocal<T>
impl<T> ThreadLocal<T>
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes and drops all entries. The TLS is considered empty then. This method is only available with exclusive references. This method is merely for optimization since the TLS is cleared at drop.
sourcepub fn iter(&self) -> Iter<'_, T> ⓘwhere
T: Sync,
pub fn iter(&self) -> Iter<'_, T> ⓘwhere
T: Sync,
Creates an iterator over immutable refereces of entries.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘwhere
T: Send,
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘwhere
T: Send,
Creates an iterator over mutable refereces of entries.
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Accesses the entry for the current thread. No initialization is performed.
sourcepub fn get_with_id(&self, id: ThreadId) -> Option<&T>
pub fn get_with_id(&self, id: ThreadId) -> Option<&T>
Accesses the entry for the current thread with a given cached ID. Repeated calls with cached IDs should be faster than reloading the ID everytime. No initialization is performed.
sourcepub fn with_init<F>(&self, init: F) -> &Twhere
F: FnOnce() -> T,
pub fn with_init<F>(&self, init: F) -> &Twhere
F: FnOnce() -> T,
Accesses the entry for the current thread. If necessary, the init
closure is called to initialize the entry.
sourcepub fn with_id_and_init<F>(&self, id: ThreadId, init: F) -> &Twhere
F: FnOnce() -> T,
pub fn with_id_and_init<F>(&self, id: ThreadId, init: F) -> &Twhere
F: FnOnce() -> T,
Accesses the entry for the current thread with a given cached ID.
Repeated calls with cached IDs should be faster than reloading the ID
everytime. If necessary, the init
closure is called to initialize the
entry.
sourcepub fn with_default(&self) -> &Twhere
T: Default,
pub fn with_default(&self) -> &Twhere
T: Default,
Accesses the entry for the current thread. If necessary, the entry is initialized with default value.
sourcepub fn with_id_and_default(&self, id: ThreadId) -> &Twhere
T: Default,
pub fn with_id_and_default(&self, id: ThreadId) -> &Twhere
T: Default,
Accesses the entry for the current thread with a given cached ID. Repeated calls with cached IDs should be faster than reloading the ID everytime. If necessary, the entry is initialized with default value.