Hello guys. I’m both a beginner in OCaml & CS, aka a noob.
I wrote this piece of code to test Thread
module
let () = print_endline "Hello, World!"
let rec fib_normal (n: int): int =
match n with
| 0 | 1 -> n
| x when x < 0 -> failwith "Negative number!"
| _ -> fib_normal (n - 1) + fib_normal (n - 2)
let () =
let _ = print_endline "With multi-threading:" in
let handle_1 = Thread.create fib_normal 43 in
let handle_2 = Thread.create fib_normal 43 in
Thread.join handle_1; Thread.join handle_2;
let _ = print_endline "Without multi-threading:" in
let _ = fib_normal 43 in let _ = fib_normal 43 in ()
As you can see, this program
-
- Create 2 threads to run fibonacci function at the same time - they should be executed at the same time, thus it should cost half of the time to finish the task
-
- Run fib once, then run fib again - this should take longer cause it did not use threads
But the result is these 2 steps took the same time to finish, and CPU usage is always < 100%, which makes me wonder if OCaml < 5.0.0 has the same limitation as CPython (Global Interpreter Lock to make Python code unable to be executed parallelly)
I have hardly any knowledge of OCaml / Computer Science so excuse me for being a noob