Commits

bergsoe committed 4c49825

[stop_after] bug fix.

Comments (0)

Files changed (1)

src/PaplStopCriteria.ml

     in make loop
 
 let stop_after time =
+  (* This procedure uses a lock to register the time-out of a thread. Using a
+     simple [bool ref] doesn't work: Ocaml can't see that the variable is
+     volatile. *)
+  let module M = BatMutex.Mutex in
+  let lock = M.create () in
+  let _ = Thread.create
+    (fun () ->
+       Thread.delay time;
+       M.lock lock) in
   let stopped = ref false in
-  let action () = Thread.delay time; !stopped = true in
-  let _ = Thread.create action ()
-  in make (fun () -> !stopped)
+  let stop () =
+    if !stopped then true
+    else if not (M.try_lock lock)
+    then begin
+      stopped := true;
+      true
+    end
+    else begin
+      M.unlock lock;
+      false
+    end
+  in make stop