Concurrency Concepts
Concurrency Concepts
Concurrency Concepts
Concurrency
Concepts
Mark Moeykens
www.bigmountainstudio.com AN VISUAL REFERENCE GUIDE
1 FOR DEVELOPERS BigConcurrency
Mountain MasteryStudio
in SwiftUI
Version: 20-NOVEMBER-2021
©2021 Big Mountain Studio LLC - All Rights Reserved
Doing Work
A central processing unit (CPU) is a physical device that handles all instructions it receives from hardware and software. This is a physical chip within
your devices. It receives tasks from your apps and executes them.
CPU
www.bigmountainstudio.com 4
Concepts
Core Core
Use width: Use width:
214 214
www.bigmountainstudio.com 5
Concepts
Each person/thread
performs tasks.
A task is a unit of
work.
Think of tasks as
a number of items
Core Core you want to
perform in your
app:
1. Disable button
after tapping
2. Get data
3. Show animation
4. Show progress
indicator
5. Show data
www.bigmountainstudio.com 6
Concepts
Main Thread
Every app has a “main thread”. This is the thread where the UI work is done, such as drawing the UI, handling scrolling or button taps.
Other threads could be used to perform tasks in the background, such as getting data or performing intensive calculations.
Go get me data
You handle drawing
from an API.
the UI, button taps,
scrolling, swiping,
After you have the
gestures, etc.
data, give it to Main
Thread to show on
the UI.
Main Thread Other Thread
Core
www.bigmountainstudio.com 7
HANDLING WORK
3
This next chapter will go over the terms and concepts for the three ways in which your code can run. Yes, there are only three ways.
Synchronous Work
1
Synchronous work means one task has to complete before the next task is started.
Concepts
1 Synchronous Work 1
A person (thread) can perform tasks in different ways. One person can do one task at one time. They will not start the next task until the first task is done.
This is called synchronous work because the second task will not be started until the first task is complete. This is normally how most of your code is run.
Tasks
1. Move gold boxes
2. Move blue boxes
Core
10 Minutes
Performance
Let’s say it takes:
1. 5 minutes to move 5 gold boxes
2. 5 minutes to move 5 blue boxes
10 minutes total
www.bigmountainstudio.com 10
Concepts
Store
Task: Get rice Task: Get vegetables Task: Get chicken Task: Get cream
www.bigmountainstudio.com 11
Concepts
Kitchen
www.bigmountainstudio.com 12
Asynchronous Work
2
Asynchronous work means that the second task can start, even though the first task has NOT completed.
But as you will see, the two tasks do NOT happen at the same time.
Concepts
2 Asynchronous Work 2
Asynchronous work is when one person splits up their time between many tasks. It is still only one person/thread.
But the person can now do part of task 1 (one box) then do part of task 2 (one box), then go back to task 1, etc.
Core
Task 1
Task 2
10 Minutes
Performance
Note: The person/thread will suspend task 1 to The advantage to doing it this way is you will
1. 5 minutes to move 5 gold boxes
work on task 2. Then suspend task 2 to work on start getting blue boxes sooner instead of
2. 5 minutes to move 5 blue boxes
task 1. waiting for ALL of the gold boxes to finish first.
Same performance (10 minutes).
www.bigmountainstudio.com 14
Concepts
2 Asynchronous Timing 2
Asynchronous work can also be expressed as tasks splitting up time, sharing time, or slicing up time to share between the tasks.
Here is another way to look at it:
1 Minute 1 Minute 1 Minute 1 Minute 1 Minute 1 Minute 1 Minute 1 Minute 1 Minute 1 Minute
Note: Even though this is expressed as 1-minute intervals, it is actually more like 7-10 nanoseconds. (A nanosecond is one billionth of a second.)
www.bigmountainstudio.com 15
Concepts
55 Minutes
www.bigmountainstudio.com 16
Concepts
2 Asynchronous Cooking 2
The previous page is not drawn to scale.
A more accurate example would really be one stove to cook on (one core processor).
For example, the chef would put the chicken on for five minutes, take it off, put the rice on for five minutes, take it off and put the vegetables on for five
minutes.
This would be repeated until one food is cooked and then it would keep rotating between the foods left.
5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes 5 minutes
Cook chicken
Vegetables finishes
finishes
55 Minutes
www.bigmountainstudio.com 17
Parallel Work
3
Parallel work means task 1 and task 2 can both start and execute at the same time.
Concepts
3 Parallel Work 3
Parallelism (parallel work) is when two or more people can perform a task at the same time. Now there is a person assigned to move gold boxes and
another for blue boxes. This is another form of concurrency (doing multiple things).
Core
Task 1
Core
Task 2
Note: The threads/people need to be on These people can move 10 boxes in 5 minutes.
different cores to make parallelism work. Better performance!
www.bigmountainstudio.com 19
Concepts
2 Minutes 2 Minutes
www.bigmountainstudio.com 20
Concepts
Task: Cook rice The lowest you can get the time
down to is 30 minutes because
of how long it takes to cook the
chicken.
30 Minutes
www.bigmountainstudio.com 21
What is Concurrency?
What is Concurrency?
The Swift language documentation on concurrency uses the term “concurrency“ to refer to this common combination of asynchronous and parallel
code. This book explores all the different ways you can create concurrent (asynchronous & parallel) code to perform tasks in your app.
Core
www.bigmountainstudio.com 23
The
End
www.bigmountainstudio.com 24