Src/main.rs

use std::rc::Rc;
use std::sync::Mutex;
use std::thread;

fn main() {
let counter = Rc::new(Mutex::new(0));
let mut handles = vec![];

for _ in 0..10 {
let counter = Rc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();

*num += 1;
});
handles.push(handle);
}

for handle in handles {
handle.join().unwrap();
}

println!(“Result: {}”, *counter.lock().unwrap());
}
Listing 16-14: Attempting to use Rc<T> to allow multiple threads to own the Mutex<T>
Once again, we compile and get… different errors! The compiler is teaching us a lot.

$ cargo run
Compiling shared-state v0.1.0 (file:///projects/shared-state)
error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
–> src/main.rs:11:36
|
11 | let handle = thread::spawn(move || {
| ————- ^——
| | |
| ______________________|_____________within this `{closure@src/main.rs:11:36: 11:43}`
| | |
| | required by a bound introduced by this call
12 | | let mut num = counter.lock().unwrap();
13 | |
14 | | *num += 1;
15 | | });
| |_________^ `Rc<Mutex<i32>>` cannot be sent between threads safely
|
= help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
note: required because it’s used within this closure
–> src/main.rs:11:36
|
11 | let handle = thread::spawn(move || {
| ^^^^^^^
note: required by a bound in `spawn`
–> /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/thread/mod.rs:728:1

For more information about this error, try `rustc –explain E0277`.
error: could not compile `shared-state` (bin “shared-state”) due to 1 previous error

$ cargo run

Compiling shared-state v0.1.0 (file:///projects/shared-state)

error[E0382]: borrow of moved value: counter

–> src/main.rs:21:29

|

5 | let counter = Mutex::new(0);

| ——- move occurs because counter has type Mutex&lt;i32&gt;, which does not implement the Copy trait

8 | for _ in 0..10 {

| ————– inside of this loop

9 | let handle = thread::spawn(move || {

| ——- value moved into closure here, in previous iteration of loop

21 | println!(“Result: {}”, *counter.lock().unwrap());

| ^^^^^^^ value borrowed here after move

|

help: consider moving the expression out of the loop so it is only moved once

|

8 ~ let mut value = counter.lock();

9 ~ for _ in 0..10 {

10 | let handle = thread::spawn(move || {

11 ~ let mut num = value.unwrap();

|

For more information about this error, try rustc --explain E0382.

error: could not compile shared-state (bin “shared-state”) due to 1 previous error


Discover more from Kvnbbg

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image
Refresh

*

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)