chapter ten

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:

10.1. Anonymous Functions

10.2. Spawning Threads

10.2.1. What does it mean to "join" threads?

10.2.2. Creating more threads takes almost no time at all

10.2.3. Effect of spawning many threads

10.2.4. Shared variable

10.3. Closures (||{}) vs functions and methods (fn)

10.4. Procedurally generated avatars from a multi-threaded parser and code generator

10.4.1. How to run render-hex and its intended output

10.4.2. Single-threaded render-hex overview

10.4.3. Spawning a thread per logical task

10.4.4. Using a thread pool and task queue

10.5. Concurrency and task virtualization

10.5.1. Threads

10.5.2. What is a context switch?

10.5.3. Processes

10.5.4. Web Assembly

10.5.5. Containers

10.5.6. Why use an operating system at all?

10.6. What you have learned