add example program from discuss

This commit is contained in:
Simon Cruanes 2024-10-03 12:43:52 -04:00
parent 6a44598a31
commit a85bc80573
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 39 additions and 0 deletions

27
examples/discuss1.ml Normal file
View file

@ -0,0 +1,27 @@
(** Example from https://discuss.ocaml.org/t/confused-about-moonpool-cancellation/15381 *)
let ( let@ ) = ( @@ )
let () =
let@ () = Trace_tef.with_setup () in
let@ _ = Moonpool_fib.main in
(* let@ runner = Moonpool.Ws_pool.with_ () in *)
let@ runner = Moonpool.Background_thread.with_ () in
(* Pretend this is some long-running read loop *)
for i = 1 to 10 do
Printf.printf "MAIN LOOP %d\n%!" i;
Moonpool_fib.check_if_cancelled ();
let _ : _ Moonpool_fib.t =
Moonpool_fib.spawn ~on:runner ~protect:false (fun () ->
Printf.printf "RUN FIBER %d\n%!" i;
Moonpool_fib.check_if_cancelled ();
Format.printf "FIBER %d NOT CANCELLED YET@." i;
failwith "BOOM")
in
Moonpool_fib.yield ();
(* Thread.delay 0.2; *)
(* Thread.delay 0.0001; *)
()
done

12
examples/dune Normal file
View file

@ -0,0 +1,12 @@
(executables
(names discuss1)
(enabled_if
(>= %{ocaml_version} 5.0))
;(package moonpool)
(libraries
moonpool
moonpool.fib
trace
trace-tef
;tracy-client.trace
))