@@ -269,6 +269,8 @@ pub struct Builder {
269
269
name : Option < String > ,
270
270
// The size of the stack for the spawned thread in bytes
271
271
stack_size : Option < usize > ,
272
+ // Skip running and inheriting the thread spawn hooks
273
+ no_hooks : bool ,
272
274
}
273
275
274
276
impl Builder {
@@ -292,7 +294,7 @@ impl Builder {
292
294
/// ```
293
295
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
294
296
pub fn new ( ) -> Builder {
295
- Builder { name : None , stack_size : None }
297
+ Builder { name : None , stack_size : None , no_hooks : false }
296
298
}
297
299
298
300
/// Names the thread-to-be. Currently the name is used for identification
@@ -348,6 +350,16 @@ impl Builder {
348
350
self
349
351
}
350
352
353
+ /// Disables running and inheriting [spawn hooks](add_spawn_hook).
354
+ ///
355
+ /// Use this if the parent thread is in no way relevant for the child thread.
356
+ /// For example, when lazily spawning threads for a thread pool.
357
+ #[ unstable( feature = "thread_spawn_hook" , issue = "none" ) ]
358
+ pub fn no_hooks ( mut self ) -> Builder {
359
+ self . no_hooks = true ;
360
+ self
361
+ }
362
+
351
363
/// Spawns a new thread by taking ownership of the `Builder`, and returns an
352
364
/// [`io::Result`] to its [`JoinHandle`].
353
365
///
@@ -472,7 +484,7 @@ impl Builder {
472
484
T : Send + ' a ,
473
485
' scope : ' a ,
474
486
{
475
- let Builder { name, stack_size } = self ;
487
+ let Builder { name, stack_size, no_hooks } = self ;
476
488
477
489
let stack_size = stack_size. unwrap_or_else ( || {
478
490
static MIN : AtomicUsize = AtomicUsize :: new ( 0 ) ;
@@ -498,7 +510,11 @@ impl Builder {
498
510
)
499
511
} ) ;
500
512
501
- let hooks = spawnhook:: run_spawn_hooks ( & my_thread) ;
513
+ let hooks = if no_hooks {
514
+ spawnhook:: ChildSpawnHooks :: default ( )
515
+ } else {
516
+ spawnhook:: run_spawn_hooks ( & my_thread)
517
+ } ;
502
518
503
519
let their_thread = my_thread. clone ( ) ;
504
520
0 commit comments