Concurrency (并发)


Quote

Concurrency is about dealing with lots of things at once, but parallelism is about doing lots of things at once.

Maximise CPU utilisation + better user experience

CPU is idle when the process and thread are performing non CPU-bounded tasks like reading and writing to IO Device and waiting a result from a remote Server etc. By performing context switch, we can let another process or thread to use CPU to complete its computation. Parallelism allows us to run multiple threads of processes at the same, if we have 4 CPU cores, it means we can have 4 processes/threads consuming the CPU at the same time.

The above describes about how concurrency helps with CPU utilisation. Concurrency also ensures users feel everything is running at the same like browsing the web and playing music at the same time.

Parallelism (并行)


Data Parallelism

  • Divide the data into smaller subsets and distribute them across different CPU cores

Important

This approach yields great performance when the operation on each subset of data is independent of the others, as each subset forms an independent subproblem.

Dividing data into 4 subsets and running them on 4 cores doesn’t yield 4 times the performance due to several factors. Overheads from task distribution, synchronisation. Amdahl’s Law limits speedup based on the proportion of sequential work, while shared memory bandwidth and cache misses can bottleneck performance. Additionally, imbalanced workloads and communication overhead further hinder scalability.

Task Parallelism

  • Distribute tasks (or Thread) across multiple CPU cores. Each thread performs a unique operation

Important

Different threads may operate on the same data, in which case Synchronisation (同步) may be needed.

If different threads operate on different data, synchronisation is not needed.

Multi-tasking


VS Time-sharing?

In Time-Sharing, we have multi-users instead of multi-tasks. Multi-tasking focuses on the tasks, and the tasks can be from different users. So in that sense, multi-tasking is a superset of time-sharing.

Time-Sharing

Info

The first time-sharing machine is invented at MIT in the early 1960s, machines before it are all Batch System.

Multics - Wikipedia was one of the first time-sharing OS which inspires the creation of Unix.

References