File: coth.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 (19 lines) | stat: -rw-r--r-- 513 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
(* Or open Thread *)
open Cothread

let lk = Mutex.create ()

let test_f () = 
  let myid = id (self ()) in
  let r = Random.int 1000 in
  for i = 0 to r do
    (*    test (Printf.sprintf "%dth test from %d\n" i myid); *)
    while (not (Mutex.try_lock lk)) do 
      Printf.printf "%d fail to get the mutex\n" myid;
    done;
    Printf.printf "%d finally get the mutex\n" myid;
    Mutex.unlock lk
  done

let ith_array = Array.init 7 (fun _ -> create test_f ()) 
let _ = Array.iter (fun t -> join t) ith_array