[concurrency-interest] Single writer multiple readers no barriers -- safe ?
nitsanw at yahoo.com
Thu Jun 5 08:14:41 EDT 2014
Q: Why you said lazySet is a valid choice for single writer. Can you give any reference or example on what can go wrong with multiple writers.
A: If you had multiple writers publishing a reference, lazySet won't work* - you'd need a CAS loop (or locking, depending on expected contention/conflict rate).
* To clarify, lazySet would work in the sense that ordering will be preserved from the single writing thread POV. If you have multiple writers to an AtomicReference using lazySet you will see one of their writes (no fairness or order between writers is guaranteed). If this is all the guarantee you need that is fine. Usually some relationship is required between the writes, leading to a stronger ordering requirement than that offered by lazySet. How you would implement that is up to you, a CAS loop, getAndSet(XCHG), getAndAdd(LOCK XADD) or locking would typically end up being used.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest