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

spand

07/04/2018, 5:31 AM
In this example of actors from the guide. What ensures the memory safety of the
counter
variable ? ie. that the latest write to
counter
is always read by the next processing thread?
Copy code
fun counterActor() = actor<CounterMsg> {
    var counter = 0 // actor state
    for (msg in channel) { // iterate over incoming messages
        when (msg) {
            is IncCounter -> counter++
            is GetCounter -> msg.response.complete(counter)
        }
    }
}
Is it the read from the mailbox that ensures the proper happens before relation?
l

louiscad

07/04/2018, 6:02 AM
The for loop is executed sequentially, so no concurrency inside it
s

spand

07/04/2018, 6:05 AM
I know, but that does not mean its memory safe. I pretty sure there has to be a read barrier somewhere and there probably is but I would prefer to see it documented.
e

Evgeniy Zaharov

07/04/2018, 7:54 AM
s

spand

07/04/2018, 8:03 AM
Thanks. That answers it all 🙂
👍 1
5 Views