QUICK TIP
QUICK TIP
MUTEXES EXPLAINED
QUICK TIP
THE FORK-JOIN PARALLELISM
OUR EXPERT
Mihalis Tsoukalos is a systems engineer and a technical writer. Find him online at @mactsouk.
Get the code for this tutorial from the Linux Format archive: www. linuxformat. com/archives ?issue=292. You can learn more about Rust at www. rust-lang.org.
You can learn more about the std::sync::mpsc module at https://doc. rust-lang. org/std/ sync/mpsc/ and about std::thread at https://doc. rust-lang.org/ std/thread/ fn.spawn. html. You can find more information about the Rayon library at https:// github.com/ rayon-rs/rayon. Finally, the Rust documentation site is at www. rust-lang.org/ learn.
A mutex is a mutually exclusive flag that enables a single thread to do something critical, in most cases changing the value of a shared variable, while the other threads are waiting for the same mutex to become available in order to get it and do their jobs on the shared variable. Therefore, a mutex allows only a single thread to have access to a piece of data at a given time. If multiple threads want to access the same data, they’ll have to wait until they get the relevant mutex. In other words, any thread that wants to access the shared data needs to acquire the lock to that mutex.
Concurrency is a way of structuring your components so that they can be executed independently when possible. In contrast, parallelism is the simultaneous execution of multiple entities. Put simply, concurrency is better than parallelism.
The thread::spawn() function that’s used for creating new threads returns a JoinHandle structure that’s “an owned permission to join on a thread”. In other words, JoinHandle provides a way to join the child thread. Usually, you’re going to use the provided JoinHandle to call the std::thread::join() method to make the main() function wait for the thread to finish. This is called fork-join parallelism because std::thread::spawn() creates a new thread whereas std::thread::join() deals with waiting for a thread to finish. The fork-join parallelism has many advantages because of its simplicity, which is its most important advantage.
QUICK TIP
Get the code for this tutorial from the Linux Format archive: www. linuxformat. com/archives ?issue=292.
You can learn more about Rust at www.
rust-lang.org.
Once the thread is done working with the shared variable or variables, it should unlock the mutex and make it available to other threads. However, Rust does the unlocking automatically so there’s no need to call an unlock function. The disadvantage of mutexes is that you need to write extra code to use them and that if you forget to unlock a mutex, your program will most likely going to misbehave or crash – again, this is not a problem with Rust.
Another method of carrying out parallel programming is Async/ Await (Asynchronous Programming). Learn more at https://rustlang.github.io/async-book/03_async_await/01_chapter.htmland at https://moodle.cs.pdx.edu/mod/page/view.php?id=491.
This month’s instalment of our ongoing Rust series will cover concurrent programming. The difficulty with concurrency can be summed up as “Shared Mutable State is the root of all problems”. However, Rust takes a different approach towards concurrency. Instead of dealing with the mutable part, Rust deals with the shared part. Put simply, the Rust compiler will let you know about any errors in the “shared” part; therefore, if your Rust code compiles you have no such errors. As a result, it’s okay to have shared memory among threads in Rust.