right, lock implementations in swift are kind of a jungle if you’re not using Foundation.
AtomicReference
is the right way to go. allocating the object is expensive, but for something that is updated in second intervals that’s not really a concern.
AtomicReference
doesn’t work with Array
because Array
is a struct wrapper around a pointer, and AtomicReference
doesn’t know how to refcount a wrapped pointer. the low-effort, low-reward answer is to wrap the array in a class. the high-effort, marginally higher-reward answer is to roll your own immutable ManagedBuffer
-backed immutable array type that contains the elements inline. but i wish it were not so difficult to do the latter.