A system is concurrent when several computations are executing at the same time and potentially interacting with each other. The computations can only run in parallel (that is, simultaneously) when they are executing on different cores or processors.
An executing Rust program consists of a collection of native Operating System (OS) threads; the OS is also responsible for their scheduling. The unit of computation in Rust is called a thread, which is a type defined in the std::thread module. Each thread has its own stack and local state.
Until now, our Rust programs only had one thread, the main thread, corresponding with the execution of the main() function. But a Rust program can create lots of threads to work simultaneously when needed. Each thread (not only main()) can act as a parent and generate any number of children threads.
Data can either be:
- Shared across threads (see the shared mutable states through atomic types section)
- Sent between threads (see the communication through channels section)