File: sample.ml

package info (click to toggle)
ocamlnet 2.2.9-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,724 kB
  • ctags: 10,053
  • sloc: ml: 63,928; ansic: 1,973; makefile: 800; sh: 651
file content (42 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download | duplicates (4)
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
(* From Patrick M Doane *)

let esys = Unixqueue.create_unix_event_system()
let after timeout f =
  let group = Unixqueue.new_group esys in
  Unixqueue.once esys group timeout f
;;

let m = Mutex.create();;
let c = Condition.create();;
  (* Unixqueue.run should not be called before any event handlers are
   * registered; it would return immediately. The condition variable
   * is used to wait until the first handler is added to the queue.
   *)

let queue_it () =
  print_endline "queue_it started"; flush stdout;
  after 4.0 (fun () -> print_endline "4 seconds"; flush stdout);
  Mutex.lock m;
  Condition.signal c;
  Mutex.unlock m;
  Thread.delay 1.0;
  print_endline "1 second"; flush stdout;
  after 1.0 (fun () -> print_endline "2 seconds"; flush stdout);  
  print_endline "queue_it finishes"; flush stdout;
;;

let run_it () =
  print_endline "run_it started"; flush stdout;
  Condition.wait c m;
  print_endline "run_it executes event system"; flush stdout;
  Unixqueue.run esys;
  print_endline "run_it finishes"; flush stdout;
;;

Mutex.lock m;;
let t1 = Thread.create queue_it ();;
let t2 = Thread.create run_it ();;

Thread.join t1;
Thread.join t2