@@ -264,6 +264,8 @@ pub struct Builder {
264
264
name : Option < String > ,
265
265
// The size of the stack for the spawned thread in bytes
266
266
stack_size : Option < usize > ,
267
+ // Skip running and inheriting the thread spawn hooks
268
+ no_hooks : bool ,
267
269
}
268
270
269
271
impl Builder {
@@ -287,7 +289,7 @@ impl Builder {
287
289
/// ```
288
290
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
289
291
pub fn new ( ) -> Builder {
290
- Builder { name : None , stack_size : None }
292
+ Builder { name : None , stack_size : None , no_hooks : false }
291
293
}
292
294
293
295
/// Names the thread-to-be. Currently the name is used for identification
@@ -343,6 +345,16 @@ impl Builder {
343
345
self
344
346
}
345
347
348
+ /// Disables running and inheriting [spawn hooks](add_spawn_hook).
349
+ ///
350
+ /// Use this if the parent thread is in no way relevant for the child thread.
351
+ /// For example, when lazily spawning threads for a thread pool.
352
+ #[ unstable( feature = "thread_spawn_hook" , issue = "none" ) ]
353
+ pub fn no_hooks ( mut self ) -> Builder {
354
+ self . no_hooks = true ;
355
+ self
356
+ }
357
+
346
358
/// Spawns a new thread by taking ownership of the `Builder`, and returns an
347
359
/// [`io::Result`] to its [`JoinHandle`].
348
360
///
@@ -465,7 +477,7 @@ impl Builder {
465
477
F : Send ,
466
478
T : Send ,
467
479
{
468
- let Builder { name, stack_size } = self ;
480
+ let Builder { name, stack_size, no_hooks } = self ;
469
481
470
482
let stack_size = stack_size. unwrap_or_else ( || {
471
483
static MIN : AtomicUsize = AtomicUsize :: new ( 0 ) ;
@@ -491,7 +503,11 @@ impl Builder {
491
503
None => Thread :: new_unnamed ( id) ,
492
504
} ;
493
505
494
- let hooks = spawnhook:: run_spawn_hooks ( & my_thread) ;
506
+ let hooks = if no_hooks {
507
+ spawnhook:: ChildSpawnHooks :: default ( )
508
+ } else {
509
+ spawnhook:: run_spawn_hooks ( & my_thread)
510
+ } ;
495
511
496
512
let their_thread = my_thread. clone ( ) ;
497
513
0 commit comments