Crates.io | lazy_thread_local |
lib.rs | lazy_thread_local |
version | 0.1.2 |
source | src |
created_at | 2023-11-13 21:23:42.698555 |
updated_at | 2024-03-11 19:39:59.675665 |
description | Lazily initialised per-object thread-local storage |
homepage | |
repository | https://github.com/YC-Lammy/lazy_thread_local |
max_upload_size | |
id | 1034061 |
size | 18,194 |
Lazy 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 crate thread_local
, this crate provides
lazy initialisation and does not depend on std.
Per-thread objects are not destroyed when a thread exits. Instead, objects
are only destroyed when the ThreadLocal
containing them is dropped.
This crate uses platform dependent methods to create thread local keys. On Unix, pthread local storage is used. On windows, Fibers storage is used. On wasm, it relies on std to provide thread id.
Basic usage of ThreadLocal
:
use lazy_thread_local::ThreadLocal;
let mut tls: ThreadLocal<u32> = ThreadLocal::new(||5);
assert_eq!(tls.get(), &5);
*tls = 6;
assert_eq!(tls.get(), &6);
Initialising ThreadLocal
in constant context:
use lazy_thread_local::ThreadLocal;
static TLS: ThreadLocal<u32> = ThreadLocal::const_new(5);
assert_eq!(TLS.get(), 5);