File: lock.ml

package info (click to toggle)
cothreads 0.10-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 500 kB
  • sloc: ml: 1,963; makefile: 216
file content (21 lines) | stat: -rw-r--r-- 574 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Thread=Cothread (* Or just use Thread, no difference *)
open Thread

let lk1 = Mutex.create ()
let lk2 = Mutex.create ()

let rec run x =
  Mutex.lock lk1;
  Printf.printf "%d takes lock 1\n" x; flush stdout;
  Mutex.lock lk2;
  Printf.printf "%d takes lock 2\n" x; flush stdout;
  Mutex.unlock lk2;
  Printf.printf "%d release lock 2\n" x; flush stdout;
  Mutex.unlock lk1;
  Printf.printf "%d release lock 1\n" x; flush stdout;
  Thread.delay (Random.float 0.2);
  run x

let _ = 
  ignore (Array.init 10 (Thread.create run));
  while true do Thread.delay 5.0 done