Skip to content

Commit 8fa9627

Browse files
committed
add more examples
1 parent 1f1dea3 commit 8fa9627

File tree

8 files changed

+70
-3
lines changed

8 files changed

+70
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["thread", "pool", "asyncwait", "primitive", "collections", "process", "syncs"]
2+
members = ["thread", "pool", "asyncwait", "primitive", "collections", "process", "syncs", "channel","special"]

channel/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "channel"
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]

channel/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

collections/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
dashmap = "5.4.0"

collections/src/lib.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{sync::{Arc, Mutex}, collections::HashMap};
2-
2+
use std::collections::LinkedList;
33

44
pub fn common_thread_safe_collections() {
55
let map: HashMap<i32, i32> = HashMap::new();
@@ -19,4 +19,45 @@ pub fn common_thread_safe_collections() {
1919
}
2020

2121
println!("HashMap: {:?}", *m.lock().unwrap());
22-
}
22+
}
23+
24+
25+
pub fn common_thread_safe_vec() {
26+
let vec1 = vec![];
27+
let vec2 = Arc::new(Mutex::new(vec1));
28+
29+
let mut handles = vec![];
30+
for i in 0..10 {
31+
let vec3 = Arc::clone(&vec2);
32+
handles.push(std::thread::spawn(move || {
33+
let mut v = vec3.lock().unwrap();
34+
v.push(i);
35+
}));
36+
}
37+
38+
for handle in handles {
39+
handle.join().unwrap();
40+
}
41+
42+
println!("vec: {:?}", vec2.lock().unwrap());
43+
}
44+
45+
pub fn common_thread_safe_linkedlist() {
46+
let list1: LinkedList<u32> = LinkedList::new();
47+
let list2 = Arc::new(Mutex::new(list1));
48+
49+
let mut handles = vec![];
50+
for i in 0..10 {
51+
let list3 = Arc::clone(&list2);
52+
handles.push(std::thread::spawn(move || {
53+
let mut v = list3.lock().unwrap();
54+
v.push_back(i);
55+
}));
56+
}
57+
58+
for handle in handles {
59+
handle.join().unwrap();
60+
}
61+
62+
println!("LinkedList: {:?}", list2.lock().unwrap());
63+
}

collections/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ use collections::*;
22

33
fn main() {
44
common_thread_safe_collections();
5+
common_thread_safe_vec();
6+
common_thread_safe_linkedlist();
7+
58
}

special/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "special"
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]

special/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

0 commit comments

Comments
 (0)