#thread #local #object

thread-local-object

Per-object thread local storage

1 unstable release

0.1.0 May 24, 2017
Download history 26/week @ 2018-12-20 1/week @ 2018-12-27 38/week @ 2019-01-03 45/week @ 2019-01-10 178/week @ 2019-01-17 68/week @ 2019-01-24 56/week @ 2019-01-31 65/week @ 2019-02-07 76/week @ 2019-02-14 82/week @ 2019-02-21 62/week @ 2019-02-28 38/week @ 2019-03-07 39/week @ 2019-03-14 82/week @ 2019-03-21 94/week @ 2019-03-28

110 downloads per month
Used in 4 crates (1 directly)

MIT/Apache

11KB
141 lines

thread-local-object

Build Status

Documentation

Per-object thread local storage.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Per-object thread local storage.

The ThreadLocal type which stores a distinct (nullable) value of some type for each thread that accesses it.

A thread's values are destroyed when it exits, but the values associated with a ThreadLocal instance are not destroyed when it is dropped. These are in some ways the opposite semantics of those provided by the thread_local crate, where values are cleaned up when a ThreadLocal object is dropped, but not when individual threads exit.

Because of this, this crate is an appropriate choice for use cases where you have long lived ThreadLocal instances which are widely shared among threads that are created and destroyed through the runtime of a program, while the thread_local crate is an appropriate choice for short lived values.

Examples

use std::sync::Arc;
use std::thread;
use thread_local_object::ThreadLocal;

let tls = Arc::new(ThreadLocal::new());

tls.set(1);

let tls2 = tls.clone();
thread::spawn(move || {
    // the other thread doesn't see the 1
    assert_eq!(tls2.get_cloned(), None);
    tls2.set(2);
}).join().unwrap();

// we still see our original value
assert_eq!(tls.get_cloned(), Some(1));

Dependencies