-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
Before we can explore the ins and outs of multithreaded programming, we must learn how to create and run code on threads. In this section, we'll explore the Rust standard library's support for threads with some additional help from the crate ecosystem.
Every program starts with a single thread, which we often call the main thread. Each thread used by the program holds its own stack and instruction pointer and, unless directed otherwise, performs its work independently of any other thread. In order to spawn new threads, we must interact with the OS and runtime facilities to create a thread and allocate resources for it. Then, we must give it some work to do. In C++, these tasks can be accomplished by the std::thread API or any number of platform-specific APIs.
Rust similarly places its thread creation functionality in the std::thread module. The easiest way to create a thread in Rust is with the std::thread::spawn...