// 100k actors: each is spawned linked to `main`, which traps exits; every // tenth one crashes. The output does not depend on the order in which // actors run, so it is checked with several scheduler threads. import std/io import std/process. {type ExitSignal, type Subject, type proc, ExitSignal, Normal} fn worker :: n: Int results: Subject -> () / { if n % 10 == 0 { panic "on purpose" } process.send results n } fn spawn_all :: n: Int results: Subject -> () / { if n > 0 { let _ = process.spawn_link fn { worker n results } spawn_all (n - 1) results } } fn sum :: results: Subject left: Int total: Int -> Int / { if left == 0 { total } else { sum results (left - 1) (total + process.receive_forever results) } } fn count :: exits: Subject left: Int normal: Int crashed: Int -> (Int, Int) / { if left == 0 { (normal, crashed) } else { match process.receive_forever exits { ExitSignal(_, Normal) -> count exits (left - 1) (normal + 1) crashed , ExitSignal(_, _) -> count exits (left - 1) normal (crashed + 1) } } } pub fn main :: -> () / { let n = 100000 process.trap_exits True let exits = process.exits () let results = process.subject () spawn_all n results io.println ("sum " <> show (sum results (n - n / 10) 0)) let counts = count exits n 0 0 io.println ("normal " <> show counts.0 <> ", crashed " <> show counts.1) } //> sum 4500000000 //> normal 90000, crashed 10000