Rust Programming language: A comprehensive beginner's guide to Rust by mEm lnc & Claudia Alves
Author:mEm lnc & Claudia Alves [lnc, mEm]
Language: eng
Format: azw3, mobi
Published: 2021-03-04T16: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
Rust Programming language: A comprehensive beginner's guide to Rust by mEm lnc & Claudia Alves.mobi
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.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7808)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6807)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6803)
Running Windows Containers on AWS by Marcio Morales(6323)
Kotlin in Action by Dmitry Jemerov(5089)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5051)
Combating Crime on the Dark Web by Nearchos Nearchou(4623)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4575)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4437)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4314)
The Age of Surveillance Capitalism by Shoshana Zuboff(3977)
Python for Security and Networking - Third Edition by José Manuel Ortega(3875)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3534)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3528)
Learn Wireshark by Lisa Bock(3491)
Mastering Python for Networking and Security by José Manuel Ortega(3376)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3353)
Blockchain Basics by Daniel Drescher(3322)
