In this example of actors from the guide. What ens...
# coroutines
s
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
The for loop is executed sequentially, so no concurrency inside it
s
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
s
Thanks. That answers it all 🙂
👍 1