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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! # Verify that Relaxed works fine when updating a counter
//!
//! Our first example describes the following scenario: There is one writer thread
//! and one reader thread, and there is only one counter being updated.
//! The ordering chosen is `Ordering::Relaxed`. And we show that, when the writer
//! increments it once, the reader might see the change or not.
//!
//! Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not impose an order among concurrent memory accesses.
//! They only guarantee atomicity and modification order consistency.
//! # Relaxed
//! Our first example describes the following scenario: There is one writer thread
//! and one reader thread, and there is only one counter being updated.
//! The ordering chosen is `Ordering::Relaxed`. And we show that, when the writer
//! increments it once, the reader might see the change or not.
//!
//! Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not impose an order among concurrent memory accesses.
//! They only guarantee atomicity and modification order consistency.
//!
//!
//! ```rust
//! use loom::sync::atomic::AtomicUsize;
//! use loom::sync::atomic::Ordering::Relaxed;
//! use loom::sync::Arc;
//! use loom::thread;
//!
//! #[cfg(test)]
//! fn relaxed_guarantees_full_completion_upon_joining_all_threads() {
//! // Typical use for relaxed memory ordering is (in/de)crementing counters,
//! // since this only requires atomicity, but not ordering or synchronization
//! loom::model(|| {
//! let num = Arc::new(AtomicUsize::new(0));
//! let num_reader = num.clone();
//!
//! let mut v = vec![];
//! for _ in 0..3 {
//! let num = num.clone();
//! v.push(thread::spawn(move || num.fetch_add(1, Relaxed)));
//! }
//!
//! for t in v {
//! t.join().unwrap();
//! }
//!
//! let num_value = num_reader.load(Relaxed);
//! assert_eq!(3, num_value)
//! });
//! }
//! ```