-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy paththreads.ml
More file actions
33 lines (28 loc) · 709 Bytes
/
threads.ml
File metadata and controls
33 lines (28 loc) · 709 Bytes
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)