OCR H446, section 2.1.5 (a)
Thinking concurrently means working out which parts of a problem can be done at the same time (concurrently), instead of one after another.
Sequential and concurrent processing
- Concurrent processing means tasks make progress during the same period. On one core they take turns (switching quickly). On several cores or processors they can run truly in parallel (1.1.1 (c)).
Which parts can run at the same time?
Two tasks can be done concurrently if they are independent: neither needs the result of the other, and they don't change the same data.
| Task | Independent? |
|---|---|
| Loading images and loading text on a web page | Yes |
| Calculating the average of each of three classes | Yes: each class is separate |
| Painting different regions of an image | Yes |
| Searching different parts of a big list | Yes |
| Adding up the marks, then dividing by the number | No: the division needs the total |
| Logging in, then loading the user's data | No: one depends on the other |
| Two people updating the same bank balance | Careful: needs coordination |
This links to the dependencies in 2.1.3 (c): steps that depend on each other must be done in order, and steps that don't can be done at the same time.
How to find the concurrent parts
- Split the problem into tasks (2.2.2 (c)).
- For each pair of tasks, ask: does one need the output of the other?
- Ask: do they use or change the same data?
- Group the independent tasks that can start together.
- Identify where results must be brought together (a synchronisation point).
Dependency diagram
The three "get data" tasks are independent and can start together. The combine step must wait for all three.
Everyday and computing examples
- A web browser downloads many files at once while showing the page.
- A video game updates graphics, sound and character movement at the same time.
- A word processor checks spelling in the background while you type.
- Weather forecasting works on different areas of the map at once.
- A web server deals with many users' requests at once.
- GPUs apply the same operation to thousands of pixels or values together.
- A cashier and a baker at a shop work at the same time on different jobs.
Writing concurrent code
In Python, threads or the concurrent.futures module can run independent tasks together:
from concurrent.futures import ThreadPoolExecutor
import time
def download(name):
time.sleep(1) # pretend this takes a second
return f"{name} downloaded"
files = ["a.jpg", "b.jpg", "c.jpg"]
with ThreadPoolExecutor() as pool:
results = list(pool.map(download, files))
print(results)
Run one after another the three downloads take about 3 seconds. Run concurrently they take about 1 second, because the waiting overlaps.
Worked example
Task: a program must work out the average mark of three separate classes, then print the average of the whole year group. Which parts can be done concurrently?
- Independent tasks: the three class averages. Each uses different data, and none needs the result of another. These can run at the same time.
- Dependent task: the year average needs all three class results, so it must wait until they are done (a synchronisation point).
- Final step: printing needs the year average, so it comes last.
With three processors, and each class average taking 2 seconds, and the year calculation and print taking 1 second: sequentially it's 2 + 2 + 2 + 1 = 7 seconds, and concurrently it's 2 + 1 = 3 seconds.
Exam tip: show you can tell independent tasks from dependent ones, and give the reason ("task B needs the output of task A, so can't start until it has finished"). Say what must wait for others to finish.
Check yourself
What does it mean to tackle parts of a problem concurrently?
Working on different parts at the same time (or overlapping in time), instead of one after another.
What makes two tasks suitable for concurrent processing?
They are independent: neither needs the result of the other, and they don't change the same data.
Give two examples of tasks that happen concurrently in a computer system
Any two of: a browser downloading several files at once; a game updating graphics and sound; a word processor checking spelling while you type; a server handling many users.
Why can't "add up the marks" and "divide by the number of marks" run at the same time?
The division needs the total, which is the result of the addition, so the second task depends on the first.