packages feed

alms-0.6.0: examples/threads4.alms

(* A semi-bad example with threads. (Possible contract error!)
    We start a thread printing 'x's, and then two threads, each
    of which can kill it:

      1. counts 4 second
      2. waits for user input

    If 2 happens first (press enter), then the program exits
    without error!  But if 1 happens first, then 2 will wait
    for input, and when it tries to kill the printer thread,
    that's a contract violation.
*)

#load "libthread"

let rec printer () =
  AThread.delay 100000;
  putStr "x";
  flush ();
  printer ()

let startStop () =
  let id = AThread.fork printer in
  let id = AThread.print id in
    (λ _ → AThread.kill id) :> unit → unit

let after delay stop =
  AThread.fork (λ _ → AThread.delay delay; stop ());
  ()

let main () =
  let stop = startStop () in
    after 2000000 stop;
    getLine ();
    stop ()    (* stop used twice! *)

in main ()