@@ -46,17 +46,17 @@ multi-threaded program below.
46
46
47
47
.. code-block :: swift
48
48
49
- import Foundation
49
+ import Foundation
50
50
51
- let queue = DispatchQueue.global (qos : .default )
51
+ let queue = DispatchQueue.global (qos : .default )
52
52
53
- class Bird {}
54
- var single = Bird ()
53
+ class Bird {}
54
+ var single = Bird ()
55
55
56
- queue.async {
57
- while true { single = Bird () }
58
- }
56
+ queue.async {
59
57
while true { single = Bird () }
58
+ }
59
+ while true { single = Bird () }
60
60
61
61
This program crashes very quickly when it tries to deallocate an already
62
62
deallocated class instance. To understand the bug try to imagine two threads
@@ -223,13 +223,13 @@ be called by worker-threads.
223
223
224
224
.. code-block :: swift
225
225
226
- func logger (_ x : Int ) {
226
+ func logger (_ x : Int ) {
227
227
228
- // I know what I'm doing!
229
- issafe {
230
- glob = x
231
- }
228
+ // I know what I'm doing!
229
+ issafe {
230
+ glob = x
232
231
}
232
+ }
233
233
234
234
235
235
Most protocols in the standard library, like `Incrementable ` and `Equatable ` are
@@ -324,20 +324,20 @@ This is an example of a tiny concurrent program that uses Tasks and Streams.
324
324
325
325
.. code-block :: swift
326
326
327
- let input = Stream< String > ()
328
- let output = Stream< String > ()
327
+ let input = Stream< String > ()
328
+ let output = Stream< String > ()
329
329
330
- func echoServer (_ inp : Stream<String >,
331
- out : Stream<String >) {
332
- while true { out.push (inp.pop ()) }
333
- }
330
+ func echoServer (_ inp : Stream<String >,
331
+ out : Stream<String >) {
332
+ while true { out.push (inp.pop ()) }
333
+ }
334
334
335
- createTask ((input, output), callback : echoServer)
335
+ createTask ((input, output), callback : echoServer)
336
336
337
- for val in [" hello" ," world" ] {
338
- input.push (val)
339
- print (output.pop ())
340
- }
337
+ for val in [" hello" ," world" ] {
338
+ input.push (val)
339
+ print (output.pop ())
340
+ }
341
341
342
342
The program above creates a server task that accepts an input stream and an
343
343
output stream that allows it to communicate with the main thread. The compiler
@@ -352,20 +352,20 @@ declaration of the streams in the closure.
352
352
353
353
.. code-block :: swift
354
354
355
- let comm : _Endpoint<String , Int > = createTask {
356
- var counter = 0
357
- while true {
358
- $0 .pop ()
359
- $0 .push (counter)
360
- counter += 1
361
- }
362
- }
363
-
364
- // CHECK: 0, 1, 2,
365
- for ss in [" " ," " ," " ] {
366
- comm.push (ss)
367
- print (" \( comm.pop () ) , " , terminator : " " )
368
- }
355
+ let comm : _Endpoint<String , Int > = createTask {
356
+ var counter = 0
357
+ while true {
358
+ $0 .pop ()
359
+ $0 .push (counter)
360
+ counter += 1
361
+ }
362
+ }
363
+
364
+ // CHECK: 0, 1, 2,
365
+ for ss in [" " ," " ," " ] {
366
+ comm.push (ss)
367
+ print (" \( comm.pop () ) , " , terminator : " " )
368
+ }
369
369
370
370
Stream utilities
371
371
----------------
@@ -403,19 +403,19 @@ Example of a concurrent program using Futures in Swift.
403
403
404
404
.. code-block :: swift
405
405
406
- func mergeSort <T : Comparable >(array : ArraySlice <T>) -> [T] {
406
+ func mergeSort <T : Comparable >(array : ArraySlice <T>) -> [T] {
407
407
408
- if array.count <= 16 { return Array (array).sorted () }
408
+ if array.count <= 16 { return Array (array).sorted () }
409
409
410
- let mid = array.count / 2
411
- let left = array[0 ..< mid]
412
- let right = array[mid..< array.count ]
410
+ let mid = array.count / 2
411
+ let left = array[0 ..< mid]
412
+ let right = array[mid..< array.count ]
413
413
414
- let lf = async (left, callback : mergeSort)
415
- let lr = async (right, callback : mergeSort)
414
+ let lf = async (left, callback : mergeSort)
415
+ let lr = async (right, callback : mergeSort)
416
416
417
- return merge (lf.await (), lr.await ())
418
- }
417
+ return merge (lf.await (), lr.await ())
418
+ }
419
419
420
420
The program above uses async to execute two tasks that sorts the two halves of
421
421
the array in parallel. Notice that the arrays in the example above are not
@@ -428,21 +428,21 @@ Here is another example of async calls using trailing closures and enums.
428
428
429
429
.. code-block :: swift
430
430
431
- enum Shape {
432
- case circle , oval , square , triangle
433
- }
431
+ enum Shape {
432
+ case circle , oval , square , triangle
433
+ }
434
434
435
- let res = async (Shape.oval ) { (c : Shape) -> String in
436
- switch c {
437
- case .circle : return " Circle"
438
- case .oval : return " Oval"
439
- case .square : return " Square"
440
- case .triangle : return " Triangle"
441
- }
442
- }
435
+ let res = async (Shape.oval ) { (c : Shape) -> String in
436
+ switch c {
437
+ case .circle : return " Circle"
438
+ case .oval : return " Oval"
439
+ case .square : return " Square"
440
+ case .triangle : return " Triangle"
441
+ }
442
+ }
443
443
444
- // CHECK: Shape: Oval
445
- print (" Shape: \( res.await () ) " )
444
+ // CHECK: Shape: Oval
445
+ print (" Shape: \( res.await () ) " )
446
446
447
447
Notice that the swift compiler infers that ``Shape `` and `String ` can be sent
448
448
between the threads.
@@ -520,11 +520,11 @@ thread-safe (explained in the previous section). For example:
520
520
521
521
.. code-block :: swift
522
522
523
- @_semantics (" swift.concurrent.async" )
524
- // This annotation tells the compiler to verify the closure and the passed arguments at the call site.
525
- public func async <RetTy , ArgsTy >(args : ArgsTy, callback : @escaping (ArgsTy) -> RetTy) -> Future<RetTy> {
526
- return unsafeAsync (args, callback : callback)
527
- }
523
+ @_semantics (" swift.concurrent.async" )
524
+ // This annotation tells the compiler to verify the closure and the passed arguments at the call site.
525
+ public func async <RetTy , ArgsTy >(args : ArgsTy, callback : @escaping (ArgsTy) -> RetTy) -> Future<RetTy> {
526
+ return unsafeAsync (args, callback : callback)
527
+ }
528
528
529
529
Example of shared data structures
530
530
---------------------------------
0 commit comments