-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcurrency.rs
36 lines (35 loc) · 1.1 KB
/
concurrency.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* In this module we will go through few examples and explain concurrency in rust lang.
* Thread example. (line 10)
* Join Handles example. (line 23)
*/
pub mod module {
use std::thread;
use std::time::Duration;
pub fn concurrency() {
// Thread example.
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
// Code executed by the main thread.
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
// Join Handles example.
let handle = thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
handle.join().unwrap();
}
}