OCR H446, section 2.1.5 (b)
Concurrent processing (2.1.5 (a)) can make a system faster and more responsive, but it isn't free. You need to be able to weigh up the gains and the costs for a given problem.
Benefits
- Faster completion. Independent tasks done together take less time than one after another, especially with several cores.
- Better use of hardware. All the cores are kept busy, and while one task waits (for the disk or network), another can use the processor.
- Responsiveness. A program can carry on responding to the user while a slow task works in the background, so the interface doesn't freeze.
- Handling many users. A server can deal with many requests at once.
- Larger problems. Work can be shared between many processors, so problems that would take too long on one become possible.
- Natural fit. Some problems are concurrent (a game with many characters, a simulation of many objects).
Trade-offs and problems
1. Not everything can be run concurrently
Tasks that depend on each other must still run in order. Some problems are mainly sequential, so there's little to gain.
2. Overhead
Creating threads or processes, dividing the work, switching between tasks, and combining the results all take time and memory. For small tasks the overhead can be greater than the saving, and concurrent code can be slower.
3. Complexity
Concurrent programs are harder to design, write, test and debug. Bugs can be intermittent: they appear only for certain timings, so they are hard to reproduce.
4. Race conditions
A race condition happens when two tasks use the same data, and the result depends on the exact order they happen to run in.
Two tasks both add 1 to a shared counter that starts at 5:
Task A reads 5 Task B reads 5
Task A writes 6 Task B writes 6 -> final value 6, not 7
One update has been lost. The fix is to make the update atomic, or to use a lock (mutex), so that only one task can use the data at a time. In a database this is record locking (1.3.2 (f)).
import threading
lock = threading.Lock()
counter = 0
def add_one():
global counter
with lock: # only one thread at a time in here
counter += 1
5. Deadlock
Deadlock happens when two or more tasks each wait for something the other holds, so none can carry on:
Both wait forever. It can be prevented by always requesting resources in the same order, or by using time-outs.
6. Sharing and synchronisation costs
Locks make tasks wait for each other, which reduces the speed gain. Tasks may need to pass data to each other (communication costs).
7. Hardware cost
More cores or processors cost more, and use more power.
Limits on the gain: Amdahl's law
Only the parallel part of a task is sped up. If a fraction of the work can be done in parallel, and there are processors, the best possible speed-up is
Example: 80% of a task can run in parallel ().
| Processors | Speed-up |
|---|---|
| 1 | 1 |
| 2 | |
| 4 | |
| 8 | |
| Very many | (the limit) |
However many processors are added, the speed-up can never exceed 5, because 20% of the work is still sequential. The sequential part limits the gain, so adding more processors gives less and less benefit.
You don't need to memorise Amdahl's law for the exam, but you should understand the idea: the part that can't be made concurrent sets a limit.
Summary of the trade-off
| Benefit | Cost | |
|---|---|---|
| Speed | Tasks complete sooner | Overhead can cancel it out |
| Responsiveness | Interface stays active | More complex program |
| Resources | Cores kept busy | More hardware and power |
| Correctness | Race conditions and deadlock | |
| Development | Harder to test and debug |
Worked example
Task: a photo editor applies a filter to a large image. Evaluate using concurrent processing.
- Why it suits concurrency: each pixel (or block of pixels) is processed independently, so the image can be split into parts, each handled by a different core. On four cores the filter could take nearly a quarter of the time.
- Responsiveness: running the filter in the background keeps the interface working so the user can cancel or carry on.
- Costs: splitting the image and joining the results is sequential work that limits the speed-up. For a small image the overhead could mean it is no faster. The code is more complex, and if different threads wrote to the same part of the output, a race condition could damage the image unless the regions are kept separate.
- Conclusion: worthwhile for large images on a multi-core machine, because the work is independent and easily split. It isn't worth the complexity for small ones.
Exam tip: a good answer gives at least one benefit and one drawback, applied to the scenario. Name the specific risk (race condition, deadlock, overhead), and explain why dependent or small tasks don't gain much.
Check yourself
Give two benefits of concurrent processing
Any two of: tasks complete sooner; better use of multiple cores; the interface stays responsive; many users can be served at once; large problems become feasible.
Give two drawbacks of concurrent processing
Any two of: overhead of creating and managing tasks; more complex to write and debug; race conditions; deadlock; some tasks can't run concurrently.
What is a race condition?
A fault where two tasks use the same data and the result depends on the order in which they happen to run, so updates can be lost.
What is deadlock?
When two or more tasks each wait for a resource that another one holds, so none of them can ever continue.
Why does adding more processors give less and less benefit?
The part of the task that must run sequentially stays the same, and it sets a limit on the total speed-up.