Skip to content

Commit e149600

Browse files
committed
episode 16
1 parent d9d8b82 commit e149600

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["./ownership-and-borrowing", "options-and-results", "errors", "iterators", "strings", "boxing", "conversions", "closures"]
2+
members = ["./ownership-and-borrowing", "options-and-results", "errors", "iterators", "strings", "boxing", "conversions", "closures", "threading"]

closures/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ fn main() {
2929
dbg!(count);
3030

3131
struct Closure<'a> {
32-
count: &'a mut u32,
32+
val: Rc<Vec<u32>>,
3333
}
3434
impl<'a> Closure<'a> {
3535
fn call(&mut self) -> u32 {
36-
self.count.add_assign(1);
37-
*self.count
36+
self.val.add_assign(1);
37+
*self.val
3838
}
3939
}
4040
}

threading/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "threading"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

threading/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![allow(unused)]
2+
3+
use std::cell::RefCell;
4+
use std::sync::{Arc, Mutex};
5+
6+
fn main() {
7+
let val = Arc::new(vec![42, 10]);
8+
let mut change = Arc::new(Mutex::new(52));
9+
let t = vec![
10+
std::thread::spawn({
11+
let val = Arc::clone(&val);
12+
let change = change.clone();
13+
move || {
14+
println!("work: {}", val[0]);
15+
*change.lock().unwrap() += 10;
16+
}
17+
}),
18+
std::thread::spawn({
19+
let val = val.clone();
20+
move || {
21+
println!("work: {}", val[1]);
22+
}
23+
}),
24+
];
25+
dbg!(&val);
26+
27+
println!("other work");
28+
for thread in t {
29+
println!("{:?}", thread.join().unwrap());
30+
}
31+
dbg!(change);
32+
}

0 commit comments

Comments
 (0)