1.2 Why use concurrency ?
There are two main reasons to use concurrency in an application:
- Separation of concerns
- Separation of concerns is almost always a good idea when writing software; by grouping related bits of code together and keeping unrelated bits of code apart, you can make your programs easier to understand and test, and thus less likely to contain bugs.
- Without the explicit use of concurrency you either have to write a task-switching framework or actively make calls to unrelated areas of code during an operation.
- Using threads in this way generally makes the logic in each thread much simpler, because the interactions between them can be limited to clearly identi-fiable points, rather than having to intersperse the logic of the different tasks
- In this case, the number of threads is independent of the number of CPU cores available, because the division into threads is based on the conceptual design rather than an attempt to increase throughput.
- Performance
- task parallelism divide a single task into parts and run each in parallel, thus reducing the total runtime.
- data parallelism each thread performs the same operation on different parts of the data.