I'm trying to implement a thread-safe lazily initialized reference. I see this sample code here:
class Image {
var _histogram: UnsafeAtomicLazyReference<Histogram> = ...
// This is safe to call concurrently from multiple threads.
var atomicLazyHistogram: Histogram {
if let histogram = _histogram.load() { return foo }
// Note that code here may run concurrently on
// multiple threads, but only one of them will get to
// succeed setting the reference.
let histogram = ...
return _histogram.storeIfNilThenLoad(foo)
}
But that code seems wrong to me. There seem to be a few issues with that code. Where is foo defined? Also can't a race happen between the call to load() and storeIfNilThenLoad(...)?