File: threads.ml

package info (click to toggle)
ocaml-luv 0.5.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,504 kB
  • sloc: ml: 11,130; makefile: 6,223; sh: 4,592; ansic: 1,517; python: 38
file content (33 lines) | stat: -rw-r--r-- 709 bytes parent folder | download | duplicates (2)
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
let () =
  let track_length = 3 in

  let rec run_tortoise = function
    | 0 ->
      print_endline "Tortoise done running!"
    | n ->
      Luv.Time.sleep 2000;
      print_endline "Tortoise ran another step";
      run_tortoise (n - 1)
  in

  let rec run_hare = function
    | 0 ->
      print_endline "Hare done running!"
    | n ->
      Luv.Time.sleep 1000;
      print_endline "Hare ran another step";
      run_hare (n - 1)
  in

  let tortoise =
    Luv.Thread.create (fun () -> run_tortoise track_length)
    |> Result.get_ok
  in

  let hare =
    Luv.Thread.create (fun () -> run_hare track_length)
    |> Result.get_ok
  in

  ignore (Luv.Thread.join tortoise);
  ignore (Luv.Thread.join hare)