Macro alloc_tls::alloc_thread_local
[−]
[src]
macro_rules! alloc_thread_local { (static $name:ident: $t: ty = $init:expr;) => { ... }; }
Declare a thread-local variable.
alloc_thread_local
declares a thread-local variable which is safe for use in implementing a
global allocator. It is invoked as:
alloc_thread_local!{ static <name>: <type> = <expr>; }
For example,
alloc_thread_local!{ static FOO: usize = 0; }
Thread-local variables follow a distinct lifecycle, and can be in one of four states: - All thread-local variables start out as uninitialized. - When a thread-local variable is first accessed, it is moved into the initializing state, and its initializer is called. - Once the initializer returns, the thread-local variable is initialized to the returned value, and it moves into the initialized state. - When the thread exits, the variable moves into the dropped state, and the variable is dropped.
Thread-local variables can be accessed using the with
method. If the variable is in the
uninitialized or initialized states, the variable can be accessed. Otherwise, it cannot,
and it is the caller's responsibility to figure out a workaround for its task that does not
involve accessing the thread-local variable.