https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Sam Garfinkel

08/19/2019, 8:41 PM
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

David Glasser

08/19/2019, 8:42 PM
is a conflating channel or flow what you're looking for?
o

octylFractal

08/19/2019, 8:46 PM
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

Sam Garfinkel

08/19/2019, 8:53 PM
Cool, thanks guys
l

Luis Munoz

08/19/2019, 8:54 PM
wouldn't volatile work for that use case
o

octylFractal

08/19/2019, 9:00 PM
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

Sam Garfinkel

08/19/2019, 9:02 PM
A conflated channel might be what I want, I want any coroutine waiting for a new update to be notified as well
l

louiscad

08/19/2019, 9:17 PM
You probably want
ConflatedBroadcastChannel
specifically.
3 Views