Recently I was reading "C++ Concurrency in Action, 2nd Edition" and took a few notes
One concurrent implementation
There are two ways to achieve concurrency:
- Multi-process: multiple single-threaded processes
- Multithreading: Use multiple threads in a single process
The above two methods have their own advantages and disadvantages, and multiple processes:
- Advantages: the data of each process will not interfere with each other, and the security is high
- Disadvantages: multiple processes require more overhead; inter-process communication is cumbersome and inefficient to implement
Multithreading:
- Advantages: less resource overhead, fast access to shared data
- Disadvantages: shared address space between threads is easy to cause data confusion, and it is necessary to deal with the problem of multiple threads accessing the same data
In most cases, C++ concurrency uses multithreading, which sacrifices a certain degree of security in exchange for higher performance .
Second, when to use concurrency
When there are the following two situations, consider using concurrency:
- Split task
- high performance
Other situations can be turned into single-threaded processing.