Skip to content

Commit 15e7ffc

Browse files
committed
add std thread examples
1 parent 726d7c6 commit 15e7ffc

File tree

10 files changed

+231
-0
lines changed

10 files changed

+231
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["thread", "future", "pool"]

future/Cargo.toml

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

future/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+
}

pool/Cargo.toml

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

pool/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+
}

thread/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

thread/Cargo.toml

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

thread/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod stdthread;
2+
3+
pub use stdthread::*;

thread/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use thread::*;
2+
3+
fn main() {
4+
start_one_thread();
5+
start_two_threads();
6+
start_thread_with_sleep();
7+
start_thread_with_priority();
8+
thread_builder();
9+
10+
start_one_thread_with_move();
11+
start_one_thread_with_thradlocal();
12+
13+
thread_park();
14+
thread_park_timeout();
15+
16+
start_scoped_threads();
17+
}
18+

thread/src/stdthread.rs

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
use std::thread;
2+
use std::time::Duration;
3+
use std::cell::RefCell;
4+
5+
use thread_priority::*;
6+
7+
pub fn start_one_thread() {
8+
let count = thread::available_parallelism().unwrap().get();
9+
println!("available_parallelism: {}", count);
10+
11+
let handle = thread::spawn(|| {
12+
println!("Hello from a thread!");
13+
});
14+
15+
handle.join().unwrap();
16+
}
17+
18+
pub fn start_two_threads() {
19+
let handle1 = thread::spawn(|| {
20+
println!("Hello from a thread1!");
21+
});
22+
23+
let handle2 = thread::spawn(|| {
24+
println!("Hello from a thread2!");
25+
});
26+
27+
handle1.join().unwrap();
28+
handle2.join().unwrap();
29+
}
30+
31+
pub fn start_thread_with_sleep() {
32+
let handle1 = thread::spawn(|| {
33+
thread::sleep(Duration::from_millis(2000));
34+
println!("Hello from a thread3!");
35+
});
36+
37+
let handle2 = thread::spawn(|| {
38+
thread::sleep(Duration::from_millis(1000));
39+
println!("Hello from a thread4!");
40+
});
41+
42+
handle1.join().unwrap();
43+
handle2.join().unwrap();
44+
}
45+
46+
pub fn start_thread_with_priority() {
47+
let handle1 = thread::spawn(|| {
48+
assert!(set_current_thread_priority(ThreadPriority::Min).is_ok());
49+
println!("Hello from a thread5!");
50+
});
51+
52+
let handle2 = thread::spawn(|| {
53+
assert!(set_current_thread_priority(ThreadPriority::Max).is_ok());
54+
println!("Hello from a thread6!");
55+
});
56+
57+
handle1.join().unwrap();
58+
handle2.join().unwrap();
59+
}
60+
61+
pub fn thread_builder() {
62+
let thread1 = ThreadBuilder::default()
63+
.name("MyThread")
64+
.priority(ThreadPriority::Max)
65+
.spawn(|result| {
66+
println!("Set priority result: {:?}", result);
67+
assert!(result.is_ok());
68+
})
69+
.unwrap();
70+
71+
let thread2 = ThreadBuilder::default()
72+
.name("MyThread")
73+
.priority(ThreadPriority::Max)
74+
.spawn_careless(|| {
75+
println!("We don't care about the priority result.");
76+
})
77+
.unwrap();
78+
79+
thread1.join().unwrap();
80+
thread2.join().unwrap();
81+
}
82+
83+
84+
pub fn start_one_thread_with_move() {
85+
let x = 100;
86+
87+
let handle = thread::spawn(move || {
88+
println!("Hello from a thread, x={}!", x);
89+
});
90+
91+
handle.join().unwrap();
92+
}
93+
94+
95+
96+
pub fn start_one_thread_with_thradlocal() {
97+
thread_local!(static COUNTER: RefCell<u32> = RefCell::new(1));
98+
99+
COUNTER.with(|c| {
100+
*c.borrow_mut() = 2;
101+
});
102+
103+
let handle1 = thread::spawn(move || {
104+
COUNTER.with(|c| {
105+
*c.borrow_mut() = 3;
106+
});
107+
108+
COUNTER.with(|c| {
109+
println!("Hello from a thread1, c={}!", *c.borrow());
110+
});
111+
});
112+
113+
let handle2 = thread::spawn(move || {
114+
COUNTER.with(|c| {
115+
*c.borrow_mut() = 4;
116+
});
117+
118+
COUNTER.with(|c| {
119+
println!("Hello from a thread2, c={}!", *c.borrow());
120+
});
121+
});
122+
123+
124+
handle1.join().unwrap();
125+
handle2.join().unwrap();
126+
127+
COUNTER.with(|c| {
128+
println!("Hello from main, c={}!", *c.borrow());
129+
});
130+
}
131+
132+
133+
pub fn thread_park() {
134+
let handle = thread::spawn(|| {
135+
thread::park();
136+
println!("Hello from a park thread!");
137+
});
138+
139+
thread::sleep(Duration::from_millis(1000));
140+
141+
handle.thread().unpark();
142+
143+
handle.join().unwrap();
144+
}
145+
146+
pub fn thread_park_timeout() {
147+
let handle = thread::spawn(|| {
148+
thread::park_timeout(Duration::from_millis(1000));
149+
println!("Hello from a park_timeout thread!");
150+
});
151+
handle.join().unwrap();
152+
}
153+
154+
pub fn start_scoped_threads() {
155+
let mut a = vec![1, 2, 3];
156+
let mut x = 0;
157+
158+
thread::scope(|s| {
159+
s.spawn(|| {
160+
println!("hello from the first scoped thread");
161+
// We can borrow `a` here.
162+
dbg!(&a);
163+
});
164+
s.spawn(|| {
165+
println!("hello from the second scoped thread");
166+
// We can even mutably borrow `x` here,
167+
// because no other threads are using it.
168+
x += a[0] + a[2];
169+
});
170+
println!("hello from the main thread");
171+
});
172+
173+
// After the scope, we can modify and access our variables again:
174+
a.push(4);
175+
assert_eq!(x, a.len());
176+
}

0 commit comments

Comments
 (0)