diff --git a/NEWS.md b/NEWS.md index aba2037c..5802ce6d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,12 @@ # History of Measurements.jl +## v2.8.0 (2022-09-01) + +### New features + +* The counter of the tags of `Measurement` objects is now atomic + ([#118](https://github.com/JuliaPhysics/Measurements.jl/pull/118)). + ## v2.7.2 (2022-05-21) ### Bug Fixes diff --git a/src/Measurements.jl b/src/Measurements.jl index 100797b7..cdee6eac 100644 --- a/src/Measurements.jl +++ b/src/Measurements.jl @@ -77,11 +77,10 @@ end @generated empty_der1(x::Measurement{T}) where {T<:AbstractFloat} = Derivatives{T}() @generated empty_der2(x::T) where {T<:AbstractFloat} = Derivatives{x}() -const tag_counters = UInt64[1] -function __init__() - nthr = Base.Threads.nthreads() - resize!(tag_counters, nthr)[:] = range(UInt64(1), step=typemax(UInt64)÷nthr, length=nthr) +# Start from 1, 0 is reserved to derived quantities +const tag_counter = Threads.Atomic{UInt64}(1) +function __init__() @require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl") @require QuadGK="1fd47b50-473d-5c70-9696-f719f8f3bcdc" include("quadgk.jl") @require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("special-functions.jl") @@ -95,7 +94,7 @@ function measurement(val::T, err::T) where {T<:AbstractFloat} if iszero(err) Measurement{T}(val, err, UInt64(0), newder) else - @inbounds tag = tag_counters[Base.Threads.threadid()] += 1 + tag = Threads.atomic_add!(tag_counter, UInt64(1)) return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T))) end end