“Is it secret Frodo? Is it SAFE?” So I have this p...
# coroutines
p
“Is it secret Frodo? Is it SAFE?” So I have this piece of code (first serious coroutine hacking I’ve done) where I have a map associating names with lists of data. The main thread will allow clients to access the lists while a daily background thread will update the data. Main thread is read-only, Background thread is write-only. I think, from an app perspective, that this code is safe even if not predictable. So am I correct?
o
only threadsafe if
data
is thread safe, I think. if
data
is accessed concurrently with a non-thread-safe-map like HashMap, it's likely to to throw ConcurrentModificationException or worse, fail silently. reason being is that you are accessing
data
on two threads, the
<http://Disp.IO|Disp.IO>
threads and your main threads. even if it's not writing on both, it's still problematic for HashMap.
👍 1
m
You should share the data by communicating, not by sharing mutable data. I think you should use an
actor
.
p
@marstran do you have a link handy?
m
You could check out Roman's talk from Kotlinconf last year:

https://www.youtube.com/watch?v=a3agLJQ6vt8

p
And I just came across
actor
in the link @octylFractal sent.
m
@octylFractal Yes, but there will be a replacement. Actors as a concept is not obsolete.
o
mhm. I only meant the specific API point
p
Yes, it is very clear from https://github.com/Kotlin/kotlinx.coroutines/issues/87 that the
actor
model is useful but could be better (support a more complex model). So it looks like a decent choice to fix my issue.
e
Sharing
MutableMap
in this way is not safe.