# Crate thread_local [−] [src]

Per-object thread-local storage

This library provides the `ThreadLocal` type which allows a separate copy of an object to be used for each thread. This allows for per-object thread-local storage, unlike the standard library's `thread_local!` macro which only allows static thread-local storage.

Per-thread objects are not destroyed when a thread exits. Instead, objects are only destroyed when the `ThreadLocal` containing them is destroyed.

A `CachedThreadLocal` type is also provided which wraps a `ThreadLocal` but also uses a special fast path for the first thread that writes into it. The fast path has very low overhead (<1ns per access) while keeping the same performance as `ThreadLocal` for other threads.

Note that since thread IDs are recycled when a thread exits, it is possible for one thread to retrieve the object of another thread. Since this can only occur after a thread has exited this does not lead to any race conditions.

# Example

    use thread_local::ThreadLocal;
    let tls: ThreadLocal<u32> = ThreadLocal::new();
    assert_eq!(tls.get(), None);
    assert_eq!(tls.get_or(|| Box::new(5)), &5);
    assert_eq!(tls.get(), Some(&5));

## Structs

CachedThreadLocal | Wrapper around `ThreadLocal` which adds a fast path for a single thread.
ThreadLocal | Thread-local variable wrapper

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)