/usr/share/doc/ocamlmakefile/examples/threads/ex1.ml is in ocamlmakefile 6.37.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | open Printf
type 'a prodcons =
{ buffer: 'a array;
lock: Mutex.t;
mutable readpos: int;
mutable writepos: int;
notempty: Condition.t;
notfull: Condition.t }
let create size init =
{ buffer = Array.create size init;
lock = Mutex.create();
readpos = 0;
writepos = 0;
notempty = Condition.create();
notfull = Condition.create() }
let put p data =
Mutex.lock p.lock;
while (p.writepos + 1) mod Array.length p.buffer = p.readpos do
Condition.wait p.notfull p.lock
done;
p.buffer.(p.writepos) <- data;
p.writepos <- (p.writepos + 1) mod Array.length p.buffer;
Condition.signal p.notempty;
Mutex.unlock p.lock
let get p =
Mutex.lock p.lock;
while p.writepos = p.readpos do
Condition.wait p.notempty p.lock
done;
let data = p.buffer.(p.readpos) in
p.readpos <- (p.readpos + 1) mod Array.length p.buffer;
Condition.signal p.notfull;
Mutex.unlock p.lock;
data
(* Test *)
let _ =
let buff = create 20 0 in
let rec produce n = put buff n; printf "%d-->\n" n; produce (n+1)
and consume () = let n = get buff in printf "-->%d\n" n; consume () in
ignore (Thread.create produce 0); consume()
|