From a85bc8057362787aa81fde017c9eab6d50c243dd Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 3 Oct 2024 12:43:52 -0400 Subject: [PATCH] add example program from discuss --- examples/discuss1.ml | 27 +++++++++++++++++++++++++++ examples/dune | 12 ++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 examples/discuss1.ml create mode 100644 examples/dune diff --git a/examples/discuss1.ml b/examples/discuss1.ml new file mode 100644 index 00000000..7554335e --- /dev/null +++ b/examples/discuss1.ml @@ -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 diff --git a/examples/dune b/examples/dune new file mode 100644 index 00000000..185cd1f9 --- /dev/null +++ b/examples/dune @@ -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 + ))