File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change 5
5
//
6
6
// The filename is a song reference; google it in quotes.
7
7
8
- fn child_generation ( gens_left : uint ) {
8
+ fn child_generation( gens_left : uint , - c : pipes :: chan < ( ) > ) {
9
9
// This used to be O(n^2) in the number of generations that ever existed.
10
10
// With this code, only as many generations are alive at a time as tasks
11
11
// alive at a time,
12
+ let c = ~mut some ( c) ;
12
13
do task:: spawn_supervised {
14
+ let c = option : : swap_unwrap ( c) ;
13
15
if gens_left & 1 == 1 {
14
16
task:: yield ( ) ; // shake things up a bit
15
17
}
16
18
if gens_left > 0 {
17
- child_generation ( gens_left - 1 ) ; // recurse
19
+ child_generation ( gens_left - 1 , c) ; // recurse
20
+ } else {
21
+ c. send ( ( ) )
18
22
}
19
23
}
20
24
}
@@ -28,5 +32,9 @@ fn main(args: ~[~str]) {
28
32
copy args
29
33
} ;
30
34
31
- child_generation ( uint:: from_str ( args[ 1 ] ) . get ( ) ) ;
35
+ let ( c, p) = pipes:: stream ( ) ;
36
+ child_generation ( uint:: from_str ( args[ 1 ] ) . get ( ) , c) ;
37
+ if p. try_recv ( ) . is_none ( ) {
38
+ fail ~"it happened when we slumbered";
39
+ }
32
40
}
You can’t perform that action at this time.
0 commit comments