# L-11 MCS 572 Fri 20 Sep 2024 : workcrew.jl """ Processing a job queue by many threads. julia -t 4 workcrew.jl to run with four threads. """ using Base.Threads nbr = 10 jobs = rand((2, 3, 4, 5, 6), nbr) println("The jobs : ", jobs) nt = nthreads() println("the number of threads : ", nt) # start nt threads with the @threads macro jobidx = Atomic{Int}(1) @threads for i=1:nt println("Worker ", threadid(), " is ready.") while true myjob = atomic_add!(jobidx, 1) if myjob > nbr break end println("Worker ", threadid(), " spends ", jobs[myjob], " seconds", " on job ", myjob, " ...") sleep(jobs[myjob]) jobs[myjob] = threadid() end end println("Jobs done : ", jobs)