a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit 29609246d206cf41bded94e62e013dc772a0416b
parent 226f7ed16ab40b054da966dc410d74ce56de5933
Author: Pablo Escobar Gaviria <gark.garcia@protonmail.com>
Date:   Wed,  5 Feb 2020 11:43:33 -0200

Fixed the syncronization issue.

Diffstat:
MOCaml/main.ml | 25++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/OCaml/main.ml b/OCaml/main.ml
@@ -31,20 +31,19 @@ let rec listen (c: bool Event.channel) (n: int) : unit =
         else listen c (n - 1)
 
 let counterexempl (max: int) (n_threads: int) : unit =
-    let sums_cache = get_sums max in
-    let c = Event.new_channel () in
+    let sums_cache = get_sums max
+    and c = Event.new_channel () in
     let counterexempl_range start =
-        let send b = let _ = Event.send c b in Thread.exit () 
-        and a = ref start in
-        
-        while !a <= max do
-            for b = 0 to !a do
-                if test !a b sums_cache then send true 
-            done;
-    
-            a := !a + n_threads
-        done;
-        send false in
+        let send b = let () = Event.sync (Event.send c b) in Thread.exit () in
+        let rec aux a = 
+            let () = 
+                for b = 0 to a do
+                    if test a b sums_cache then send true 
+                done 
+            and a_ = a + n_threads in
+            if a_ <= max then aux a_ else send false in
+
+        aux start in
     let spawn = Thread.create counterexempl_range in
     
     let _ = Array.init n_threads spawn in