# L-13 MCS 572 Wed 25 Sep 2024 : fibmt.jl """ Example from https://julialang.org/blog/2019/07/multithreading Run typing time JULIA_NUM_THREADS=8 julia fibmt.jl 10 at the command prompt to compute the 10-th Fibonacci number with tasks mapped to 8 threads. Alternatively, type time julia -t 8 fibmt.jl 10 at the command prompt. The recursive function fib illustrates the starting of a task and the synchronization of the sibling task. t = @spawn fib(n-2) starts a task to compute fib(n-2) fetch(t) waits for t to complete and gets its return value There can not be any speedup because of the only computation, the '+' happens after the synchronization. """ import Base.Threads.@spawn function fib(n::Int) if n < 2 return n end t = @spawn fib(n-2) return fib(n-1) + fetch(t) end if length(ARGS) > 0 nbr = parse(Int64, ARGS[1]) println(fib(nbr)) else println(fib(10)) end