File tree 1 file changed +39
-0
lines changed 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -424,4 +424,43 @@ mod tests {
424
424
assert ! ( !thread_check_failed. load( Ordering :: Acquire ) ) ;
425
425
assert_eq ! ( count. load( Ordering :: Acquire ) , 200 ) ;
426
426
}
427
+
428
+ #[ test]
429
+ fn test_nested_spawn ( ) {
430
+ let pool = TaskPool :: new ( ) ;
431
+
432
+ let foo = Box :: new ( 42 ) ;
433
+ let foo = & * foo;
434
+
435
+ let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
436
+
437
+ let outputs: Vec < i32 > = pool. scope ( |scope| {
438
+ for _ in 0 ..10 {
439
+ let count_clone = count. clone ( ) ;
440
+ let scope = scope. clone ( ) ;
441
+ scope. clone ( ) . spawn ( async move {
442
+ for _ in 0 ..10 {
443
+ let count_clone_clone = count_clone. clone ( ) ;
444
+ scope. spawn ( async move {
445
+ if * foo != 42 {
446
+ panic ! ( "not 42!?!?" )
447
+ } else {
448
+ count_clone_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
449
+ * foo
450
+ }
451
+ } ) ;
452
+ }
453
+ * foo
454
+ } ) ;
455
+ }
456
+ } ) . collect ( ) ;
457
+
458
+ for output in & outputs {
459
+ assert_eq ! ( * output, 42 ) ;
460
+ }
461
+
462
+ // the inner loop runs 100 times and the outer one runs 10. 100 + 10
463
+ assert_eq ! ( outputs. len( ) , 110 ) ;
464
+ assert_eq ! ( count. load( Ordering :: Relaxed ) , 100 ) ;
465
+ }
427
466
}
You can’t perform that action at this time.
0 commit comments