The Memory Safety chapter under the Characteristics of Memory Access subsection in the Swift Programming Language book defines an access as instantaneous if “it’s not possible for other code to run after that access starts but before it ends.”

I’m having a hard time comprehending the meaning of this statement and feel like I’m losing my mind and forgetting how the English language works.

I really just have confusion about what “but before it ends” means. I will reword this statement to more clearly convey what I think it means, and why “but before it ends” confuses me:

… it’s not possible for other code to run after that access starts, but it is possible for code to run before that access ends.

I’m probably misunderstanding something here, but this is the only way I can make the phrase “but before it ends” make sense here. If this sentence is an accurate rewording, my question would then be: “How can code not be able to run after an access starts, and also be able to run before it ends.” Is that what this is saying?

If anyone could rewrite this statement in an abundantly clear way for me that would be incredibly helpful, or if I’m right in my understanding, show me what I’m missing.

1

There are 1 best solutions below

0
OhadM On

It can be confusing. Here's a possible clarification:

When it says "it's not possible for other code to run after that access starts but before it ends," what it means is that during the time period when the access is happening, no other code can modify or access the same memory location. This is to ensure that the access is atomic and that there are no race conditions.

For example, suppose you have two threads of execution, T1 and T2, both of which access the same memory location. If T1 starts accessing the memory location first, and T2 tries to access it while T1 is still accessing it, you might get unexpected behavior since T2 might see an inconsistent view of the memory location. To prevent this, Swift makes sure that no other code can access the same memory location while T1 is accessing it. Once T1 finishes accessing the memory location, then T2 can access it if it needs to.

Bottom line, what the sentence means is that no other code can modify or access the same memory location during an access operation, but other code can run before or after the access operation.