Skip to content

Commit ac5ce6a

Browse files
committed
Apply minor fixes from hardcopy self edit.
Parallel and async code, by itself, doesn't give you memory safety. That's one of the reasons to use Swift concurrency -- to get memory safety in your code. Expand the "you can't do this" to give some follow-on advice about what you can do, in terms of adoption. Avoid the wording "even stronger" when there isn't an obvious guarantee being compared to.
1 parent 336cb26 commit ac5ce6a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

TSPL.docc/LanguageGuide/Concurrency.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ for example, a computer with a four-core processor
1616
can run four pieces of code at the same time,
1717
with each core carrying out one of the tasks.
1818
A program that uses parallel and asynchronous code
19-
carries out multiple operations at a time;
20-
it suspends operations that are waiting for an external system,
21-
and makes it easier to write this code in a memory-safe way.
19+
carries out multiple operations at a time,
20+
and it suspends operations that are waiting for an external system.
2221

2322
The additional scheduling flexibility from parallel or asynchronous code
2423
also comes with a cost of increased complexity.
@@ -345,6 +344,14 @@ so you can call it from synchronous code and wait for the result.
345344
The Swift standard library intentionally omits this unsafe functionality,
346345
and trying to implement it yourself can lead to
347346
problems like subtle races, threading issues, and deadlocks.
347+
When adding concurrent code to an existing project,
348+
work from the top down.
349+
Specifically,
350+
start by converting the topmost layer of code to use concurrency,
351+
and then start converting the functions and methods that it calls,
352+
working through the project's architecture one layer at a time.
353+
There's no way to take a bottom-up approach,
354+
because synchronous code can't ever call asynchronous code.
348355

349356
<!--
350357
OUTLINE
@@ -655,7 +662,7 @@ Each task checks whether it has been canceled
655662
at the appropriate points in its execution,
656663
and responds to cancellation an appropriate way.
657664
Depending on the work you're doing,
658-
that usually means one of the following:
665+
responding to cancellation usually means one of the following:
659666

660667
- Throwing an error like `CancellationError`
661668
- Returning `nil` or an empty collection
@@ -1044,7 +1051,7 @@ only code running on an actor can access that actor's local state.
10441051
This guarantee is known as *actor isolation*.
10451052

10461053
The following aspects of the Swift concurrency model
1047-
combine to make it easier to reason about shared mutable state:
1054+
work together to make it easier to reason about shared mutable state:
10481055

10491056
- Code in between possible suspension points runs sequentially,
10501057
without the possibility of interruption from other concurrent code.
@@ -1071,7 +1078,7 @@ extension TemperatureLogger {
10711078
}
10721079
```
10731080

1074-
The code above converts one measurement at a time.
1081+
The code above converts the array of measurements, one at a time.
10751082
While the map operation is in progress,
10761083
some temperatures are in Fahrenheit and others are in Celsius.
10771084
However, because none of the code includes `await`,
@@ -1083,8 +1090,10 @@ This means there's no way for other code
10831090
to read a list of partially converted temperatures
10841091
while unit conversion is in progress.
10851092

1086-
The `convertFarenheitToCelsius()` method makes an even stronger guarantee:
1087-
It's a synchronous method,
1093+
In addition to writing code in an actor
1094+
that protects temporary invalid state by omitting potential suspension points,
1095+
you can move that code into a synchronous method.
1096+
The `convertFarenheitToCelsius()` method above is method,
10881097
so it's guaranteed to *never* contain potential suspension points.
10891098
This function encapsulates the code
10901099
that temporarily makes the data model inconsistent,

0 commit comments

Comments
 (0)