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

Creates an empty thread local storage.

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.

Creates an iterator over immutable refereces of entries.

Creates an iterator over mutable refereces of entries.

Accesses the entry for the current thread. No initialization is performed.

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.

Accesses the entry for the current thread. If necessary, the init closure is called to initialize the entry.

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.

Accesses the entry for the current thread. If necessary, the entry is initialized with default value.

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.

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.