Rust Programming language: A comprehensive beginner's guide to Rust by lnc mEm & Alves Claudia

Rust Programming language: A comprehensive beginner's guide to Rust by lnc mEm & Alves Claudia

Author:lnc, mEm & Alves, Claudia [lnc, mEm]
Language: eng
Format: epub, pdf
Published: 2021-03-05T00:00:00+00:00


Threads

The standard Rust library provides a library for thread handling, which allows you to run rust code in parallel. Here is a basic example of using std :: thread :

use std :: thread;

fn main () {

thread :: spawn (|| {

println! ( "Hello from a thread!" );

});

}

The thread :: spawn () method accepts a closure as an argument, a closure that is executed on a new thread. thread :: spawn () returns a handle to the new thread, which can be used to wait for the thread to finish and then extract its result:

use std :: thread;

fn main () {

let handle = thread :: spawn (|| {

"Hello from a thread!"

});

println! ( "{}" , handle.join (). unwrap ());

}

Many languages ​​possess the ability to run threads, but it is wildly insecure. There are whole books on how to prevent mistakes that occur as a result of sharing mutable state. Rust helps with its rate system, preventing race conditions at compile time. Let's talk about how you can effectively share things between threads.

Mutable State Shared Safe

Due to Rust's type system, we have a concept that sounds like a lie: "safe shared mutable state". Many developers agree that the shared mutable state is very, very bad.

Someone once said:

The shared mutable state is the root of all evil. Most languages ​​try to deal with this problem through the 'mutable' part, but Rust confronts it by solving the 'shared' part.

The same membership that prevents improper use of pointers also helps eliminate race conditions, one of the worst concurrency-related bugs.

As an example, a Rust program that would have a career condition in many languages. In Rust I would not compile:

use std :: thread;

fn main () {

let mut data = vec! [1u32, 2, 3];

for i in 0..3 {

thread :: spawn (move || {

data [i] + = 1;

});

}

thread :: sleep_ms (50);

}

The above produces an error:

8:17 error: capture of moved value: `data`

data [i] + = 1;

^ ~~~

In this case, we know that our code should be safe, but Rust is not sure about it. It is not really safe, if we had a reference to data in each thread, and the thread belongs to the reference, we would have three owners! That is wrong. We can fix this by using the Arc <T> type , a pointer with atomic reference count. The 'atomic' part means that it is safe to share between threads.

Arc <T> assumes one more property about its content to ensure that it is safe to share between threads: it assumes that its content is Sync . But in our case, we want to mutate the value. We need a guy who can make sure that only one person at a time can mutate what's inside. For that, we can use the Mutex <T> type . Here is the second version of our code. It still doesn't work, but for a different reason:

use std :: thread;

use std :: sync :: Mutex;

fn main () {

let mut data = Mutex :: new (vec! [1u32, 2, 3]);

for i in 0..3 {

let data = data.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.