100% found this document useful (6 votes)
96 views

Full Download Modern Concurrency on Apple Platforms: Using async/await with Swift 1st Edition Andrés Ibañez Kautsch PDF DOCX

Apple

Uploaded by

huzaimrehaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (6 votes)
96 views

Full Download Modern Concurrency on Apple Platforms: Using async/await with Swift 1st Edition Andrés Ibañez Kautsch PDF DOCX

Apple

Uploaded by

huzaimrehaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Download Full Version ebook - Visit ebookmeta.

com

Modern Concurrency on Apple Platforms: Using


async/await with Swift 1st Edition Andrés Ibañez
Kautsch

https://ebookmeta.com/product/modern-concurrency-on-apple-
platforms-using-async-await-with-swift-1st-edition-andres-
ibanez-kautsch/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmeta.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

Modern Concurrency on Apple Platforms: Using async/await


with Swift 1st Edition Andrés Ibañez Kautsch

https://ebookmeta.com/product/modern-concurrency-on-apple-platforms-
using-async-await-with-swift-1st-edition-andres-ibanez-kautsch/

ebookmeta.com

Statistical Analysis with Swift: Data Sets, Statistical


Models, and Predictions on Apple Platforms Andersson

https://ebookmeta.com/product/statistical-analysis-with-swift-data-
sets-statistical-models-and-predictions-on-apple-platforms-andersson/

ebookmeta.com

Chess Explained The c3 Sicilian 1st Edition Sam Collins

https://ebookmeta.com/product/chess-explained-the-c3-sicilian-1st-
edition-sam-collins/

ebookmeta.com

The Art of Elam Ca 4200 525 BC Javier Álvarez Mon

https://ebookmeta.com/product/the-art-of-elam-ca-4200-525-bc-javier-
alvarez-mon/

ebookmeta.com
Equity Visits A New Approach to Supporting Equity Focused
School and District Leadership 1st Edition Rachel Roegman

https://ebookmeta.com/product/equity-visits-a-new-approach-to-
supporting-equity-focused-school-and-district-leadership-1st-edition-
rachel-roegman/
ebookmeta.com

The Sound of Silence Leave Me Breathless 1st Edition


Dakota Willink

https://ebookmeta.com/product/the-sound-of-silence-leave-me-
breathless-1st-edition-dakota-willink/

ebookmeta.com

The New Asian Cookbook: From Seoul to Jakarta Discover


Authentic Oriental Recipes 2nd Edition Booksumo Press

https://ebookmeta.com/product/the-new-asian-cookbook-from-seoul-to-
jakarta-discover-authentic-oriental-recipes-2nd-edition-booksumo-
press/
ebookmeta.com

Process Intensification and Integration for Sustainable


Design 1st Edition Mahmoud M. El-Halwagi (Editor)

https://ebookmeta.com/product/process-intensification-and-integration-
for-sustainable-design-1st-edition-mahmoud-m-el-halwagi-editor/

ebookmeta.com

Lucky Day The Holiday Series 1st Edition Sammi Starlight

https://ebookmeta.com/product/lucky-day-the-holiday-series-1st-
edition-sammi-starlight/

ebookmeta.com
Choosing Her: An mc curvy girl instalove romance (Skin
Sins Tattoo Shop Book 2) 1st Edition Jailaa West

https://ebookmeta.com/product/choosing-her-an-mc-curvy-girl-instalove-
romance-skin-sins-tattoo-shop-book-2-1st-edition-jailaa-west/

ebookmeta.com
Modern
Concurrency on
Apple Platforms
Using async/await with Swift

Andrés Ibañez Kautsch
Modern Concurrency
on Apple Platforms
Using async/await with Swift

Andrés Ibañez Kautsch


Modern Concurrency on Apple Platforms: Using async/await with Swift

Andrés Ibañez Kautsch


La Paz, Bolivia

ISBN-13 (pbk): 978-1-4842-8694-4 ISBN-13 (electronic): 978-1-4842-8695-1


https://doi.org/10.1007/978-1-4842-8695-1

Copyright © 2023 by Andrés Ibañez Kautsch


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Mark Powers
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1
New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201)
348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on the GitHub repository: https://github.com/Apress/Modern-Concurrency-
on-Apple-Platforms. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
To my mother Renata and to my brother Gastón, for all the
support and patience they have always shown and for
everything they have always done for me.
Table of Contents
About the Author���������������������������������������������������������������������������������ix

About the Technical Reviewer�������������������������������������������������������������xi

Acknowledgments�����������������������������������������������������������������������������xiii

Preface�����������������������������������������������������������������������������������������������xv

Chapter 1: Introduction������������������������������������������������������������������������1
Important Concepts to Know���������������������������������������������������������������������������������2
Threads������������������������������������������������������������������������������������������������������������3
Concurrency and Asynchronous Programming�����������������������������������������������3
Multithreading Pitfalls�������������������������������������������������������������������������������������6
Existing Multithreading and Concurrency Tools���������������������������������������������13
Introducing async/await��������������������������������������������������������������������������������20
Requirements������������������������������������������������������������������������������������������������������21
Summary������������������������������������������������������������������������������������������������������������22
Exercises�������������������������������������������������������������������������������������������������������������23

Chapter 2: Introducing async/await���������������������������������������������������25


Closures and Their Place in Concurrency and Multithreading����������������������������25
Getting Started with async/await������������������������������������������������������������������������34
The async keyword����������������������������������������������������������������������������������������35
The await keyword����������������������������������������������������������������������������������������37
Using async/await�����������������������������������������������������������������������������������������39

v
Table of Contents

async get properties��������������������������������������������������������������������������������������49


async/await in iOS 13 and iOS 14�����������������������������������������������������������������50
Summary������������������������������������������������������������������������������������������������������������50
Exercises�������������������������������������������������������������������������������������������������������������51

Chapter 3: Continuations��������������������������������������������������������������������53
Understanding Continuations������������������������������������������������������������������������������54
Converting closure-based calls into async/await������������������������������������������54
Converting delegate-based code into async/await���������������������������������������58
Supporting async/await in iOS 13 and 14�����������������������������������������������������66
Summary������������������������������������������������������������������������������������������������������������72
Exercises�������������������������������������������������������������������������������������������������������������72

Chapter 4: Structured Concurrency����������������������������������������������������73


Understanding Structured Concurrency��������������������������������������������������������������75
The async let construct���������������������������������������������������������������������������������75
Task Groups���������������������������������������������������������������������������������������������������78
Summary������������������������������������������������������������������������������������������������������������86
Exercises�������������������������������������������������������������������������������������������������������������86

Chapter 5: Unstructured Concurrency������������������������������������������������89


Tasks in Depth�����������������������������������������������������������������������������������������������������89
Creating Tasks�����������������������������������������������������������������������������������������������90
Unstructured Concurrency in Action��������������������������������������������������������������93
The Task Tree����������������������������������������������������������������������������������������������������100
Error Propagation����������������������������������������������������������������������������������������103
Task Cancellation����������������������������������������������������������������������������������������103
Task Cancellation and Task Groups�������������������������������������������������������������111

vi
Table of Contents

Unstructured Concurrency with Detached Tasks����������������������������������������������112


Summary����������������������������������������������������������������������������������������������������������113
Exercises�����������������������������������������������������������������������������������������������������������114

Chapter 6: Actors������������������������������������������������������������������������������117
Introducing Actors���������������������������������������������������������������������������������������������118
Interacting with an Actor�����������������������������������������������������������������������������������120
Nonisolated Access to an Actor�������������������������������������������������������������������125
Actors and Protocol Conformance��������������������������������������������������������������������127
Actor Reentrancy����������������������������������������������������������������������������������������������129
Actors and Detached Tasks�������������������������������������������������������������������������������133
General Tips for Working with Actors����������������������������������������������������������������133
Summary����������������������������������������������������������������������������������������������������������135

Chapter 7: Sendable Types���������������������������������������������������������������137


Understanding Sendable Types�������������������������������������������������������������������������137
The Sendable Protocol���������������������������������������������������������������������������������139
Summary����������������������������������������������������������������������������������������������������������149

Chapter 8: The Main Actor & Final Actors����������������������������������������151


The Main Thread�����������������������������������������������������������������������������������������������151
The Main Actor��������������������������������������������������������������������������������������������153
Understanding Global Actors�����������������������������������������������������������������������������160
Creating Global Actors���������������������������������������������������������������������������������160
Summary����������������������������������������������������������������������������������������������������������165

Chapter 9: AsyncSequence���������������������������������������������������������������167
Introducing AsyncSequence������������������������������������������������������������������������������167
A Short Dive into Sequences and AsyncSequences������������������������������������169
AsyncSequence Concrete Types������������������������������������������������������������������170

vii
Table of Contents

AsyncSequence Example����������������������������������������������������������������������������������171
Native APIs That Use AsyncSequence���������������������������������������������������������177
The AsyncStream Object�����������������������������������������������������������������������������������177
The CoreLocationAsyncStream Project�������������������������������������������������������178
The AsyncThrowingStream Object��������������������������������������������������������������������190
Summary����������������������������������������������������������������������������������������������������������190

Chapter 10: @TaskLocal�������������������������������������������������������������������191


Introducing the @TaskLocal Property Wrapper�������������������������������������������������192
Using @TaskLocal���������������������������������������������������������������������������������������192
Summary����������������������������������������������������������������������������������������������������������196

Index�������������������������������������������������������������������������������������������������197

viii
About the Author
Andrés Ibañez Kautsch started writing iOS
apps as a young college student in 2011. His
first introduction to concurrency programming
and its common pitfalls was in an operating
systems class that introduced the importance
(and complexity) of writing concurrent code.
Since then, he has studied how this problem
is solved in Apple’s platforms, including
iOS. Andy has worked in institutions that
make use of concurrent technologies to keep
their services running for their customers, including banks, applying the
concepts to their mobile applications.

ix
About the Technical Reviewer
Massimo Nardone has more than 22 years
of experience in security, web/mobile
development, and cloud and IT architecture.
His true IT passions are security and Android.
He has been programming and teaching
how to program with Android, Perl, PHP, Java,
VB, Python, C/C++, and MySQL for more than
20 years.
He holds a Master of Science degree in
Computing Science from the University of
Salerno, Italy.
He has worked as a project manager, software engineer, research
engineer, chief security architect, information security manager,
PCI/SCADA auditor, and senior lead IT security/cloud/SCADA architect
for many years.

xi
Acknowledgments
Whenever you decide to buy a technical book, you usually see one or two
author names, and maybe a reviewer attached to it, but there are a lot of
people involved in a single book, both directly and indirectly. It will not
be possible to acknowledge everyone who has helped make this book
possible, but I’d like to single out a few.
First, I’d like to give a big kudos to Alexis Marechal. Alexis has, in
short, saved my career as a software developer. Alexis is not only a great
teacher, but also a great friend. His teaching style has greatly inspired
mine, and I hope I can do justice to that in this book. A big thank-you
to Carlos Anibarro and Jose Luis Vera for their teachings and words of
encouragement as well.
Writing a book is something I have always wanted to do, but Ernesto
Campohermoso is the first person who ever asked me, “When are you
going to write a book?” That single question was a great push for this book
to come into existence.
Ivan Galarza is the first person I mentored in iOS development, and he
became a very reliable iOS engineer in a short time thanks to all the talent
he has. He was the ideal candidate to review a book in progress. His input
helped me make sure everything makes sense and that this book will be
something people would like to read.
A big thank-you to all the folks at Apress for the process that made
writing this book possible. The entire experience has been a joy, from
being contacted to write a book to turning in the final chapters. The final
form of this book would have not been possible without their tenacity and
amazing work ethic.

xiii
Acknowledgments

Finally, I’d like to give a big thanks to all the readers of my blog,
andyibanez.com. The comments and feedback I received in the original
tutorial series for this topic helped me greatly improve this book. I cannot
reply to every message I get, but I am thankful to all my readers. Thank
you all.

xiv
Preface
Concurrency is a topic a lot of developers find scary. And for good reason.
Concurrency is probably one of the most complicated topics in the world
of programming. When you look at it from a very high level, concurrency
allows us to do a lot of work at the same time. Sometimes related,
sometimes unrelated. The definition is simple, but as a programmer, it is
hard to reason about concurrent programs. We learn to write code that
can be read from top to bottom, and code usually makes sense when you
read it in such a manner. In fact, the way developers reason about code
is not too different to how non-programmers reason about a cooking
recipe. Humans can understand anything they read if it is structured and
makes sense.
But the very nature of concurrency breaks this top-to-bottom reading
flow. Not only do we need to reason about concurrency differently, but we
also need to keep in mind other factors that make it harder, such as shared
mutable data, locks, threads…. Concurrency is naturally very complicated,
especially when dealing with lower-level tools.
Luckily, the new async/await system, introduced in 2021, makes
concurrency easier to reason about because it abstracts a lot of the
complexity behind language-integrated features that are easy to use and
hard to misuse. If you have written concurrent code before with anything
other than async/await in any other platform (and that includes but is
not limited to iOS, macOS, and other Apple platforms), you do not have
to concern yourself with mutexes, semaphores, or any other low-level
concurrency primitives. If you have never written concurrent code before,
you can write amazing multithreaded software in a way that makes
sense without ever having to concern yourself with the complexities of

xv
Preface

traditional concurrency. This new system is very powerful and easy to use
for both developers familiar with concurrency as well as those who have
never written concurrent code before.

Who This Book Is For


This book is aimed at intermediate iOS developers and above. You should
find this book to be of your skill level if you have been writing Apple apps
a bit over a year. Having experience with other concurrency tools in either
Apple platforms or anything else may help you grasp this book easier, but
previous concurrency knowledge is by no means necessary. You should be
familiar with the basic process of writing and maintaining an iOS app to
take advantage of this book.

How This Book Is Organized


I tried my best to organize this book in a way that makes sense. There were
topics that recursively required the knowledge of other topics before they
could be properly understood. For those situations, I spent a little bit more
time explaining some concepts at a higher level so you could get by before
they got properly introduced.
Chapter 1 introduces concurrency and its traditional problems when
trying to implement it. It discusses low-level concurrency primitives and
how they can be used. It also discusses the traditional problems you will
find when you try to implement a concurrency system without using
higher-level tools such as async/await.
Chapter 2 formally introduces “async” and “await” as keywords of the
Swift language. These two keywords are essential to understand to use the
concurrency system effectively. Every single topic makes use of async/
await, so this chapter is completely dedicated to these keywords.

xvi
Preface

Chapter 3 introduces Continuations, a tool that helps you migrate


closure-based or even delegate-based code to use async/await. This
can help you “bridge” such code into the async/await world, making
them easier to write and understand. You will also learn how to backport
concurrent code to iOS 14 and 13.
Chapter 4 introduces the concept of Structured Concurrency. You will
write your first concurrent code here. Structured concurrency helps you
write multithreaded code that is easy to read and write.
Chapter 5 introduces the concept of Unstructured Concurrency, a
topic that will help you write streamlined concurrent code with a little bit
more of flexibility than Structured Concurrency.
Chapter 6 introduces the concept of Actors. Actors are reference types
that isolate their own state, so they are useful when you need to write
concurrent code that deals with shared mutable state. It helps you answer
questions such as “What happens if two processes write to this variable at
the same time?”
Chapter 7 is all about Sendable types, which are objects that can
be used safely in concurrent code, either because they have built-in
protection (such as actors) or because the developers took special care of
these types to make them usable concurrently (like classes that implement
their own synchronization mechanism).
Chapter 8 discusses Global Actors, a tool to help you write concurrent
code that is spread out across different files and even frameworks. It also
discusses the Main Actor, a global actor that you use when you need to
update your app’s UI.
Chapter 9 is all about async sequences. These sequences can help
you receive values over time in an asynchronous context, helping you
eliminate the usage of closures and delegates in some scenarios.
Chapter 10, the final chapter, covers the usage of a property wrapper
called @TaskLocal, which you can use to share data down a concurrent
tasks tree.

xvii
Preface

Before You Get Started


While Apple managed to backport the new concurrency system to iOS 13
and iOS 14, it is recommended you study this system with iOS 15. There
are no native APIs that use async/await in lower iOS versions, and you
would need to provide an alternative to them every time you are interested
in using them.
It is recommended you have at least Xcode 13, but you should have
the latest version if possible. At the time of this writing, the latest Xcode
version is 13.4.1. The exercises and sample code were tested on this Xcode
version. This implies your Mac will need to run macOS Monterey as Xcode
13 cannot run on anything lower than Monterey.

xviii
CHAPTER 1

Introduction
Programmers are used to writing programs that are executed in a linear
fashion. As you write, test, and execute your program, you expect your
instructions to run in the order that you wrote them. In Listing 1-1, you
have a program that will first assign a variable a to the number 2. It will
then assign a variable b to the number 3, followed by assigning a variable
sum, the sum of a + b, and it will finally print a result to the console. There
is no way this program will work if you try to print(sum) before you even
managed to calculate the value for sum.

Listing 1-1. A simple program that runs from top to bottom

let a = 2
let b = 3
let sum = a + b // This variable depends on the values for
a and b, but the variables themselves can be assigned in
any order.
print(sum) // We can only print a variable that we have the
value of.

This is called procedural programming, because you write simple


statements, and they are executed from top to bottom. Even if you add
statements that can alternate the execution flow, it’s still easy to follow.
If you a call function when working with procedural programming, your
program will “jump” to a different place in memory and execute its

© Andrés Ibañez Kautsch 2023 1


A. I. Kautsch, Modern Concurrency on Apple Platforms,
https://doi.org/10.1007/978-1-4842-8695-1_1
Chapter 1 Introduction

contents, but the execution of these lines will also be done procedurally in
the same order they were written until control flow is returned to the caller.
Even people who are not programmers can follow any instruction set if
they are written in a specific order and if they are doing one thing at a time.
Someone following a cooking recipe, or someone building a doghouse
from an online tutorial may not be a programmer, but people are naturally
good at doing something if they have the steps clearly laid down.
But computer programs grow and become more complex. While it is
true that a lot of complex software can be written that follows such a linear
execution flow, often programs will need to start doing more than one
thing at once; rather than having a clear code execution path that you can
follow with your bare eyes, your program may need to execute in such a
way that it’s not obvious to tell what’s going on at a simple glance of the
source code. Such programs are multithreaded, and they can run multiple
(and often – but not always – unrelated) code paths at the same time.
In this book, we will learn how to use Apple’s async/await
implementation for asynchronous and multithreaded programming. In
this chapter, we will also talk about older technologies Apple provides for
this purpose, and how the new async/await system is better and helps you
to not concern yourself with traditional concurrency pitfalls.

Important Concepts to Know


Concurrency and asynchronous programming are very wide topics. While
I’d love to cover everything, it would go out of the scope of this book.
Instead, we will define four important concepts that will be relevant while
we explore Apple’s async/await system, introduced in 2021. We will
define them with as few words as possible, because it’s important that you
keep them in mind while you work through the chapters of this book. The
concepts of the new system itself will be covered in the upcoming chapters.

2
Chapter 1 Introduction

Note Apple is not the original creator of the async/await


system. The technology has been used in other platforms in the
past. Microsoft announced C# would get async/await support in
2011, and C# with these features was officially released to the public
in 2012.

Threads
The concept of Thread can vary even when talked about in the same
context (in this case, concurrency, and asynchronous programming).
In this book, we will treat a thread as a unit of work that can run
independently. In iOS, the Main Thread runs the UI of your app, so every
UI-related task (updating the view hierarchy, removing and adding views)
must take place in the main thread. Attempting to update the UI outside of
the main thread can result in unwanted behavior or, even worse, crashes.
In low-level multithreading frameworks, multithreading developers
will manually create threads and they need to manually synchronize
them, stop them, and do other thread management operations on their
own. Manually handling threads is one of the hardest parts of dealing with
multithreading in software.

Concurrency and Asynchronous Programming


Concurrency is the ability of a thread (or your program) to deal with
multiple things at once. It may be responding to different events, such as
network handlers, UI event handlers, OS interruptions, and more. There
may be multiple threads and all of them can be concurrent.

3
Chapter 1 Introduction

There are different APIs throughout Apple’s SDKs that make use of
concurrency. Listing 1-2 shows how to request permission to use Touch ID
or Face ID, depending on the device.

Listing 1-2. Biometric unlock is an asynchronous task

func requestBiometricUnlock() {
    let context = LAContext()

    var error: NSError? = nil

    let canEvaluate = context.canEvaluatePolicy(


.deviceOwnerAuthenticationWithBiometrics, error: &error)

    if canEvaluate {
        if context.biometryType != .none {
            // (1)
            context.evaluatePolicy(
                .deviceOwnerAuthenticationWithBiometrics,
                localizedReason: "To access your data") {
(success, error) in
                // (2)
                if success {
                    // ...
                }
            }
        }
    }
}

(1) calls context.evaluatePolicy, which is a concurrent call. This


will ask the system to suspend your app so it can take over. The system will
request permission to use biometrics while your app is suspended. The

4
Chapter 1 Introduction

thread your app was running on may be doing something entirely different
and not even related to your app while the system is running context.
evaluatePolicy. When the user responds to the prompt, either accepting
or rejecting the biometric request, it will deliver the result to your app. The
system will wait for an appropriate time to notify your app with the user’s
selection. The selection will be delivered to your app in the completion
handler (also called a callback) on (2), at which point your app will be in
control of the thread again. The selection may be delivered in a different
thread than the one which launched the context.evaluatePolicy call –
this is important to know, because if the response updates the UI, you
need to do that work on the main thread. This is also called a blocking
mechanism or interruption, as evaluatePolicy is a blocking call for the
thread. If you have done iOS for at least a few months now, you are familiar
with this way of dealing with various events. URLSession, image pickers,
and more APIs make use of this mechanism.
People often think that asynchronous programming is the act
of running multiple tasks at once. This is a different concept called
Multithreading, and we will talk about it in the next point.

Note If you are thinking on implementing biometric unlock to


resources within your app, please don’t use the code above. It has
been simplified to explain how concurrency works, and it doesn't
have the right safety measures to protect your user's data.

Multithreading is the act of running multiple tasks at once. Multiple


threads (hence its name – multithreading) are usually involved. Many tasks
can be running at the same time in the context of your app. Downloading
multiple images from the internet at the same time or downloading a file
from your web browser while you open some tabs are some examples of
multithreading. This allow us to run tasks in parallel and is sometimes
called parallelism.

5
Chapter 1 Introduction

Multithreading Pitfalls
Concurrency and multithreading are traditionally hard problems to solve.
Ever since their introduction in the computer world, developers have had
to develop paradigms and tools to make dealing with concurrency easier.
Because programmers are used to thinking procedurally, writing code that
executes at unexpected times is hard to get right.
In this section we will talk about some of the problems developers
who write low-level multithreading code often face, and the models
they have created to work with them. It is important you understand this
section because these traditional problems are real, but their solutions
are already implemented in the async/await system. It will also help you
decide which technology you should use next time you need to implement
concurrency or multithreading in your programs.

Deadlocks
In the context of multithreading, a deadlock occurs when two different
processes are waiting on the other to finish, effectively making it
impossible for any of them to finish to begin with.
This can happen when both processes are sharing a resource. If
Thread B wants a resource that Thread A is holding, and Thread A wants a
resource that Thread B has, both processes will be waiting for each other to
finish, sitting in a perpetual deadlock state. Figure 1-1 illustrates how this
might occur.

6
Chapter 1 Introduction

Figure 1-1. Deadlocking in action

Figure 1-1 illustrates how Thread A may try to access Resource C and
Resource D, one after the other, and how Thread B may try to do the same
but in different order. In this example, the deadlock will happen rather
soon, because Thread A will get hold of Resource C while Thread B gets
hold of Resource D. Whoever needs each other’s resource first will cause
the deadlock.
Despite how simple this problem looks, it is the culprit of a lot of bugs
in a lot of multithreaded software. The simple solution would be to prevent
each process from attempting to access a busy resource. But how can this
be achieved?

Solving the Deadlocking Problem


The deadlock problem has many established solutions. Mutex and
Semaphores being the most used ones. There is also Inter-process
communication through pipes, but we will not talk about it because it goes
beyond a single program.

7
Chapter 1 Introduction

Mutex

Mutex is short for Mutually exclusive lock (or flag). A mutex will signal
other processes that a process is currently using some resource by adding
a lock to it and preventing other processes from grabbing until that lock is
freed. Ideally, a process will acquire a lock to all the resources it will need
at once, even before using them. This way, if Thread A needs Resource C
and Resource D, it can lock them before Thread B tries to access them.
Thread B will wait until all the locks are freed before attempting to access
the resources itself. Figure 1-2 illustrates how this is done.

Figure 1-2. Using a mutex

Keep in mind that in this specific situation, it means that Thread A


and Thread B, while multithreaded, will not run strictly at the same time,
because Thread B needs the same resources as Thread A and it will wait for
them to be free. For this reason, it is important to identify tasks that can be
run in parallel before designing a multithreaded system.
8
Chapter 1 Introduction

Semaphores

A Semaphore is a type of lock, very similar to a mutex. With this solution, a


task will acquire a lock to a resource. Any other tasks that arrive and need
that resource will see that the resource is busy. When the original thread
frees the resources, it will signal interested parties that the resource is
free, and they will follow the same process of locking and signaling as they
interact with the resource.
Mutex and Semaphores sound similar, but the key difference lies in
what kind of resource they are locking, and if you ever find yourself in
a situation in which you need to decide what to protect (you won’t find
such a case when using async/await), it is important to think about what
makes sense in your use case. In general, a mutex can be used to protect
anything that doesn’t have any sort of execution on its own, such as
files, sockets, and other filesystem elements. Semaphores can be used to
protect execution in your program itself such us shared code execution
paths. These can be mutating functions or classes with shared state. There
may be cases in which executing a function by more than one thread will
have unintended consequences, and semaphores are a good tool for that.
For example, a function that writes a log to disk may be protected with a
semaphore because if multiple processes write to the log at the same time,
the log will become corrupted for the end user. Whoever has a semaphore
to the logging function needs to signal the other interested parties that it is
free so they can continue their execution.
A rule of thumb is to always add a timeout whenever a semaphore
is acquired and let processes timeout if they take too long. If this is
acceptable in the context of your program (no data corruption can
occur, or there can be no other unintentional consequences to canceling
tasks), consider adding a timeout so the semaphore is free again after a
certain period.

9
Chapter 1 Introduction

The use of semaphores will allow threads to synchronize, as they will


be able to coordinate the resources amongst each other so they both have
their fair usage of them.

Starvation
The Starvation problem happens when a program is stuck in a state of
perpetually waiting for resources to do some work but never receiving
them. Figure 1-3 illustrates this problem.

Figure 1-3. The dining philosophers problem

Figure 1-3 describes what is known as the dining philosophers problem,


and it is a classical representation of the starvation problem.
The problem illustrates five philosophers who want to eat at a shared
table. All of them have their own dishes, and there are five forks. However,
each philosopher needs two forks to eat. This means that only two

10
Chapter 1 Introduction

philosophers get to eat, and only the ones who are not sitting directly next
to each other. When a philosopher is not eating, he puts both forks down.
But there can be a situation in which a philosopher decides to overeat,
leaving another to starve.
Luckily for low-level multithreading developers, it’s possible to use
the semaphores we talked about above to solve this problem. The idea is
to have a semaphore that keeps track of the used forks, so it can signal
another philosopher when a fork is free to be used.

Race Conditions
Likely the multithreading problem most developers are familiar with,
Race Conditions are very similar to deadlocks, with the exception
that they can cause data or memory corruption that will lead to other
unpleasant consequences. When we are dealing with multithreading,
it isn’t inherently a bad thing that two processes are reading data at the
same time. If the resource is read-only, there is no harm. However, if the
processes can modify or update the resource in any way, the processes
will continually overwrite the data another process just wrote, and this
will eventually lead to reading and writing corrupted data. If the resource
in question is user data, we will have unhappy users. If the resource is
provided by the OS, we can cause other unwanted consequences and
eventually reach one of the multithreading issues such as deadlocks.
Figure 1-4 illustrates this pitfall.

11
Chapter 1 Introduction

Figure 1-4. Multiple threads writing to the same file at the same time

If a history log is being written to by any threads with no control, events


may be registered in a random order, and one thread may override the
line another thread just wrote. For this reason, the History Log should be
locked at the writing operation. A thread writing to it will get a mutex to the
log, and it will free it after it’s done using it. Anyone else needing to write to
the log will be waiting until the lock is freed.
Deadlocks and race conditions are very similar. The main difference,
in deadlocks, is we have multiple processes waiting for the other to finish,
whereas in race conditions we have both processes writing data and
corrupting it because no processes know that the resource in question is
being used by someone else. This means that the solutions for deadlocking
also apply to race conditions, so just block your resource with a mutex or
semaphore to guarantee exclusive access to the resource by one process
only. This is called an Atomic Operation.

12
Chapter 1 Introduction

Livelocks
There is a saying in life that goes “you can have too much of a nice thing.”
It is important to understand that throwing locks willy-nilly is not a
solution to all multithreading problems. It may be tempting to identify a
multithreading issue, throw a mutex at it, and call it a day. Unfortunately,
it’s not so easy. You need to understand your problem and identify where
the lock should go.
Similarly, this problem can occur when processes are too lenient
with the conditions under which they can release and attain resources. If
Thread A can ask Thread B for a resource and Thread B complies without
any thought, and Thread B can do the same to Thread A, it may happen
that eventually they will not be able to ask each other for the resource back
because they can’t even get to that point of the execution.
This is a classic example in real life. Suppose you are walking towards a
closed door and someone else arrives to it at almost the same time as you.
You may want to let the other person go in first, but they may tell you that
you should go first instead. If you are familiar with this awkward feeling,
you know what a livelock feels like for our multithreaded programs.

Existing Multithreading and Concurrency Tools


Apple offers some lower-level tools to write multithreaded and concurrent
code. We will show examples for some of them. They are listed from lower
level to higher level. The lower the level, the harder it is to use that system
correctly, and the less likely it is that you will encounter it in the real world
anyway. You should be aware of these tools so you can be prepared to work
with them if you ever need to.

13
Chapter 1 Introduction

pthreads
pthreads (POSIX Threads) are the implementation of a standard defined
by the IEEE.1 The POSIX part of their name (Portable Operating System
Interface) tells us that they are available in many platforms, and not only
on Apple’s operating systems. Traditionally, hardware vendors used to sell
their products offering their own, proprietary multithreading APIs. Thanks
to this standard you can expect to have a familiar interface in multiple
POSIX systems. The great advantage of pthreads is that they are available
in a wide array of systems if they are POSIX compliant, including some
Linux distributions.
The disadvantage is that pthreads are purely written in C. C is a very
low-level programming language, and it is a language that is slowly fading
from the knowledge base of many developers. The younger they are, the
less likely they are to know C. While I do not expect C to disappear any
time soon, the truth is that it’s very hard to find iOS developers who know
the C programming language, let alone use it correctly. pthreads are the
lowest-level multithreading APIs we have available, and as such they
have a very steep learning curve. If you opt to use pthreads, it’s because
you have highly specific needs to control the entire multithreading and
concurrency flow, though most developers will not need to drop down to
these levels often, if at all. You will be launching threads and managing
resources such as mutex more manually. If you are familiar with pthreads
because you worked with them in another platform, you can use your
knowledge here too, but be aware future programmers who will maintain
your code may not be familiar with either pthreads or the C language itself.

1
IEEE POSIX 1003.1c standard (1995) – https://standards.ieee.org/
ieee/1003.1c/1393/

14
Discovering Diverse Content Through
Random Scribd Documents
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law in
the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms of
this agreement by keeping this work in the same format with its
attached full Project Gutenberg™ License when you share it without
charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears, or
with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.

You might also like