apple / swift-migration-guide

Apache License 2.0
283 stars 19 forks source link

Data Race Safety on Atomicity leaves me with a doubt #124

Open bjo0 opened 1 month ago

bjo0 commented 1 month ago

https://www.swift.org/migration/documentation/swift-6-concurrency-migration-guide/dataracesafety#Atomicity

Because the current isolation domain is freed up to perform other work, actor-isolated state may change after an asynchronous call. As a consequence, you can think of explicitly marking potential suspension points as a way to indicate the end of a critical section.

func deposit(pineapples: [Pineapple], onto island: Island) async {
var food = await island.food
food += pineapples
await island.store(food)
}

This code assumes, incorrectly, that the island actor’s food value will not change between asynchronous calls. Critical sections should always be structured to run synchronously.

The phrase This code assumes, incorrectly leaves me with a major doubt about what I'm supposed to be taking away from this section. What's the correct way to use suspension points as a way to indicate the end of a critical section?

mattmassicotte commented 1 month ago

Just to make sure I understand, are you saying the last sentence is unclear?

Critical sections should always be structured to run synchronously.

bjo0 commented 1 month ago

The code block and subsequent sentence seem like a non sequitur of the prior sentences.

I was expecting to see an example of how to use explicitly marking potential suspensions points as a way to indicate the end of a critical section. I can see why the code block is problematic; but I'd like to know how to do it correctly.