Function add_spawn_hook
fn add_spawn_hook<F, G>(hook: F)
where
F: 'static + Send + Sync + Fn(&Thread) -> G,
G: 'static + Send + FnOnce()
Registers a function to run for every newly thread spawned.
The hook is executed in the parent thread, and returns a function that will be executed in the new thread.
The hook is called with the Thread handle for the new thread.
The hook will only be added for the current thread and is inherited by the threads it spawns. In other words, adding a hook has no effect on already running threads (other than the current thread) and the threads they might spawn in the future.
Hooks can only be added, not removed.
The hooks will run in reverse order, starting with the most recently added.
Usage
add_spawn_hook;
Example
A spawn hook can be used to "inherit" a thread local from the parent thread:
use Cell;
thread_local!
// This needs to be done once in the main thread before spawning any threads.
add_spawn_hook;
X.set;
spawn.join.unwrap;