What's the proper data structure to store a synchr...
# coroutines
s
What's the proper data structure to store a synchronized reference to only the most recent value that I can share between a coroutine and any other thread? Should I just use an atomic object or is there something more specialized?
d
is a conflating channel or flow what you're looking for?
o
I'm not sure a conflated channel works for storing the reference -- the channel will lose the most recent value after it is received. Probably atomic object is the best option here -- for simple get/set I don't think the blocking time is long enough to warrant suspending for it.
s
Cool, thanks guys
l
wouldn't volatile work for that use case
o
hmm, I suppose it would -- Atomic.get/set is basically a volatile read/write. But that does remind me of issues with volatile reference fields: https://stackoverflow.com/questions/23855677/java-volatile-reference-to-mutable-object-will-updates-to-the-objects-field so perhaps you may need to add a
Mutex
or other such synchronization, to ensure safe publication
s
A conflated channel might be what I want, I want any coroutine waiting for a new update to be notified as well
l
You probably want
ConflatedBroadcastChannel
specifically.