-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_let.rs
More file actions
64 lines (52 loc) · 1.67 KB
/
sync_let.rs
File metadata and controls
64 lines (52 loc) · 1.67 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
An example that describes how to quickly create an anonymous
sync with a mutable variable.
This code creates 5 threads, each of which tries to update
the `sync_let` variable with data while executing the synchronized anonymous code.
*/
#[cfg(not(feature = "async"))]
fn main() {
use std::thread::spawn;
use synchronized::sync;
// An array of handles to wait for all threads to complete.
let mut join_all = Vec::new();
// Creation of 5 threads to implement a multi-threaded environment.
for thread_id in 0..5 {
let join = spawn(move || {
// Create anonymous synchronized code with one mutable variable `sync_let` and `count`.
let result = sync!(
(sync_let: String = String::new(), count: usize = 0) {
// If it's the first thread,
// then theoretically `sync_let` is String::new().
if thread_id == 0 {
assert!(sync_let.is_empty());
assert_eq!(count, &0);
}
// We fill the variable `sync_let` and `count` with data.
sync_let.push_str(&thread_id.to_string());
sync_let.push(' ');
*count += 1;
sync_let.clone()
}
);
// Outputting debug information.
println!("#[id: {}] {}", thread_id, result);
});
// In order for our `assert_eq!(sync_let.is_empty());` code to
// always run correctly, the first thread should always run first
// (this is just for the stability of this example).
if thread_id == 0 {
let _e = join.join();
continue;
}
join_all.push(join);
}
// We just wait for all threads to finish and look at stdout.
for tjoin in join_all {
let _e = tjoin.join();
}
}
#[cfg(feature = "async")]
fn main() {
println!("This example only builds and runs with --feature=\"sync\"");
}