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