From 6975ce901f41e1a1e57115c27589b1071f3ac251 Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 14 Oct 2023 10:18:05 -0700 Subject: [PATCH] init_multi: Be more thread-safe --- src/cluster.jl | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/cluster.jl b/src/cluster.jl index ac6e3f2..e676af7 100644 --- a/src/cluster.jl +++ b/src/cluster.jl @@ -1318,18 +1318,16 @@ end using Random: randstring -let inited = false - # do initialization that's only needed when there is more than 1 processor - global function init_multi() - if !inited - inited = true - push!(Base.package_callbacks, _require_callback) - atexit(terminate_all_workers) - init_bind_addr() - cluster_cookie(randstring(HDR_COOKIE_LEN)) - end - return nothing +# do initialization that's only needed when there is more than 1 processor +const inited = Threads.Atomic{Bool}(false) +function init_multi() + if !Threads.atomic_cas!(inited, false, true) + push!(Base.package_callbacks, _require_callback) + atexit(terminate_all_workers) + init_bind_addr() + cluster_cookie(randstring(HDR_COOKIE_LEN)) end + return nothing end function init_parallel()