osiire’s blog

ふしぎなそふとやさん

0から小さい順に整数を返す関数をスレッドセーフに

今度はconcurrent cellを使ってみる。

(* to compile, ocamlfind ocamlc -thread -package ccell -linkpkg sf.ml *)
let (@@) f g = f g
let (+>) g f = f g
let tee f x = ignore (f x); x

open Ccell
open Event

let num =
  let m = 
    Mvar.init_make 0
  in
  fun () ->
    sync (Mvar.take m)
    +> tee (fun x -> sync @@ Mvar.put m (x + 1))

let _ =
  print_int (num ());
  print_int (num ());
  print_int (num ());
  print_int (num ());
  print_newline()