10 Processes, Threads and Containers
So far this book has almost completely avoided two fundamental terms of systems programming: threads and processes. Instead, the book has used the single term: program. This chapter expands our vocabulary.
Processes, threads and containers are abstractions created to enable multiple tasks to be carried out at the same time. They enable concurrency. Its peer term, parallelism, means to make use of multiple physical CPU cores at the same time.
Counter-intuitively, it is possible to have a concurrent system on a single CPU core. Because accessing data from memory and I/O take a very long time, threads requesting data can be set to a “blocked” state. Blocked threads are re-scheduled when their data is available.
Concurrency, or doing multiple things at the same time, is difficult to introduce into a computer program. Employing concurrency effectively involves both new concepts new syntax.
The aim of this chapter is to give you the confidence to explore more advanced material. You will have a solid understanding of the different tools that you have available to you as an applications programmer. It exposes you to standard library and the well engineered crates crossbeam
and rayon
. It won’t give you sufficient background to be able to implement your own concurrency crates, but will enable you to use them.
The chapter follows the following structure: