Member-only story
An introduction into Rust. Part 14: Multi Threading

In this post we will look at the support of Rust for multi threading. As you probably know, CPU-heavy computations like we often are faced to when dealing with scientific algorithms, sometimes can be implemented to run in parallel. Multi threading must be not be mixed up with asynchronous programming which is a totally different concept and mainly focuses on reducing waiting times of a thread when having to deal with IO operations.
In general parallelization is simply to achieve in Rust. Though, the concept of ownership and lifetime are more strikingly as ever and therefore have to be regarded carefully.
Prerequisites to understand this story do all lean on the previous chapters of my series.
Multi threading:
The following code spans a new thread and makes the main thread waiting on it to be finished by calling join
on its handle
:
use std::thread;let mut x = 1;let handle = thread::spawn(move || {
x = 7;
println!("{}", x); // 7…