Spawning and configuring threads
In the previous section, we reviewed the fundamentals of multi-threading that apply broadly to all user processes in the Unix environment. There is, however, another aspect of threading that is dependent on the programming language for implementation – this is the threading model.
Rust implements a 1:1 model of threading where each operating system thread maps to one user-level thread created by the Rust Standard Library. The alternative model is M:N (also known as green threads) where there are M green threads (user-level threads managed by a runtime) that map to N kernel-level threads.
In this section, we'll cover the fundamentals of creating 1:1 operating system threads using the Rust Standard Library. The Rust Standard Library module for thread-related functions is std::thread
.
There are two ways to create a new thread using the Rust Standard Library. The first method uses the thread::spawn
function, and the second method uses...