Published Cell
Published Cell
In this lab, build a small lock-free publication primitive using atomic.Pointer.
Function signatures
type PublishedCell struct { ... }
func NewPublishedCell() *PublishedCell
func (c *PublishedCell) Store(value int)
func (c *PublishedCell) Load() (value int, ok bool)
func (c *PublishedCell) Swap(value int) (previous int, ok bool)
Contract
Storepublishes a new immutable snapshot.Loadreturns(0, false)when no value has been published yet.Swapreturns the previous published value andtruewhen one exists; returns(0, false)if empty.- The API must be safe for concurrent readers and writers.
- Readers must never observe a partially initialized state.
- Avoid additional synchronization where atomics are sufficient.
Notes
- Use
sync/atomicpointer APIs. - Keep behavior deterministic for nil receiver calls.
- Do not panic on empty cells.
Complexity
Store,Load,Swap:O(1).
Run tests to see results
No issues detected