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 @@ -406,4 +406,43 @@ mod tests {
406
406
assert ! ( !thread_check_failed. load( Ordering :: Acquire ) ) ;
407
407
assert_eq ! ( count. load( Ordering :: Acquire ) , 200 ) ;
408
408
}
409
+
410
+ #[ test]
411
+ fn test_nested_spawn ( ) {
412
+ let pool = TaskPool :: new ( ) ;
413
+
414
+ let foo = Box :: new ( 42 ) ;
415
+ let foo = & * foo;
416
+
417
+ let count = Arc :: new ( AtomicI32 :: new ( 0 ) ) ;
418
+
419
+ let outputs: Vec < i32 > = pool. scope ( |scope| {
420
+ for _ in 0 ..10 {
421
+ let count_clone = count. clone ( ) ;
422
+ let scope = scope. clone ( ) ;
423
+ scope. clone ( ) . spawn ( async move {
424
+ for _ in 0 ..10 {
425
+ let count_clone_clone = count_clone. clone ( ) ;
426
+ scope. spawn ( async move {
427
+ if * foo != 42 {
428
+ panic ! ( "not 42!?!?" )
429
+ } else {
430
+ count_clone_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
431
+ * foo
432
+ }
433
+ } ) ;
434
+ }
435
+ * foo
436
+ } ) ;
437
+ }
438
+ } ) . collect ( ) ;
439
+
440
+ for output in & outputs {
441
+ assert_eq ! ( * output, 42 ) ;
442
+ }
443
+
444
+ // the inner loop runs 100 times and the outer one runs 10. 100 + 10
445
+ assert_eq ! ( outputs. len( ) , 110 ) ;
446
+ assert_eq ! ( count. load( Ordering :: Relaxed ) , 100 ) ;
447
+ }
409
448
}
You can’t perform that action at this time.
0 commit comments