// `demonitor`: no `Down` arrives after it returns; `demonitor_flush` also // drops one that arrived before. import std/io import std/process.{type Pid, type Subject, type proc, Down} /// An actor that waits until told to return. fn waiting :: -> (Pid, Subject<()>) / { let ready = process.subject () let pid = { use <- process.spawn let go = process.subject () process.send ready go process.receive_forever go } (pid, process.receive_forever ready) } /// Whether a message waits on `down`. fn pending :: down: Subject -> String / { match process.receive down 0 { Some(_) -> "a Down", None -> "nothing" } } pub fn main :: -> () / { // Demonitored before the exit: no `Down`, the other monitor's comes. let (pid, go) = waiting () let down = process.monitor pid let other = process.monitor pid process.demonitor pid down process.send go () let Down(_, _) = process.receive_forever other io.println ("demonitored: " <> pending down) // After the exit: the `Down` already there stays, unless flushed. let (pid, go) = waiting () let kept = process.monitor pid let flushed = process.monitor pid let other = process.monitor pid process.send go () let Down(_, _) = process.receive_forever other process.demonitor pid kept process.demonitor_flush pid flushed io.println ("after the exit: " <> pending kept <> ", flushed: " <> pending flushed) // `spawn_monitor`'s monitor too; twice, or another pid: nothing more. let (pid, down) = { use <- process.spawn_monitor process.sleep 10 } process.demonitor pid down process.demonitor pid down process.demonitor (process.self ()) down let other = process.monitor pid let Down(_, _) = process.receive_forever other io.println ("spawn_monitor: " <> pending down) // A pid that is gone: `monitor` answers `noproc` at once, and it stays. let down = process.monitor pid process.demonitor pid down io.println ("gone: " <> pending down) // Only the subject's owner can demonitor. let (pid, _) = waiting () let down = process.monitor pid let _ = { use <- process.spawn process.demonitor pid down } process.sleep 10 io.println ("from another actor: " <> pending down) } //> demonitored: nothing //> after the exit: a Down, flushed: nothing //> spawn_monitor: nothing //> gone: a Down //> from another actor: nothing