From 35c1a8ac41b204e41b39115d23294b0becbe8432 Mon Sep 17 00:00:00 2001 From: MarcMush Date: Fri, 19 Jan 2024 14:46:43 +0100 Subject: [PATCH] add locks in test_threads --- test/test_threads.jl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/test_threads.jl b/test/test_threads.jl index 3b17d1e..e16c70a 100644 --- a/test/test_threads.jl +++ b/test/test_threads.jl @@ -22,11 +22,14 @@ prog = ProgressMeter.ProgressUnknown(desc="Attempts at exceeding trigger:") vals = Float64[] threadsUsed = Int[] + lk = ReentrantLock() Threads.@threads for _ in 1:1000 !in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid()) - push!(vals, rand()) - valssum = sum(vals) - if sum(vals) <= trigger + valssum = lock(lk) do + push!(vals, rand()) + return sum(vals) + end + if valssum <= trigger ProgressMeter.next!(prog) elseif !prog.done ProgressMeter.finish!(prog) @@ -47,8 +50,10 @@ threadsUsed = Int[] Threads.@threads for _ in 1:100000 !in(Threads.threadid(), threadsUsed) && push!(threadsUsed, Threads.threadid()) - push!(vals, -rand()) - valssum = sum(vals) + valssum = lock(lk) do + push!(vals, -rand()) + return sum(vals) + end if valssum > thresh ProgressMeter.update!(prog, valssum) else