Skip to content

Commit dc41cf1

Browse files
committed
add chapter 11
1 parent f4c58f0 commit dc41cf1

27 files changed

+3624
-6
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crossbeam::atomic::AtomicCell;
2+
3+
4+
pub fn atomic_cell_example() {
5+
let a = AtomicCell::new(0i32);
6+
7+
a.store(1);
8+
assert_eq!(a.load(), 1);
9+
10+
11+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
12+
assert_eq!(a.fetch_add(1), 2);
13+
assert_eq!(a.load(), 3);
14+
assert_eq!(a.swap(100), 3);
15+
assert_eq!(a.load(), 100);
16+
assert_eq!(a.into_inner(), 100);
17+
18+
let a = AtomicCell::new(100i32);
19+
let v = a.take();
20+
assert_eq!(v, 100);
21+
assert_eq!(a.load(), 0);
22+
}
23+
24+
25+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crossbeam::atomic::AtomicCell;
2+
3+
4+
pub fn atomic_cell_example() {
5+
let a = AtomicCell::new(0i32);
6+
7+
a.store(1);
8+
assert_eq!(a.load(), 1);
9+
10+
11+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
12+
assert_eq!(a.fetch_add(1), 2);
13+
assert_eq!(a.load(), 3);
14+
assert_eq!(a.swap(100), 3);
15+
assert_eq!(a.load(), 100);
16+
assert_eq!(a.into_inner(), 100);
17+
18+
let a = AtomicCell::new(100i32);
19+
let v = a.take();
20+
assert_eq!(v, 100);
21+
assert_eq!(a.load(), 0);
22+
}
23+
24+
25+
pub fn atomic_consume_example() {
26+
use crossbeam::atomic::AtomicConsume;
27+
28+
let data = vec![1, 2, 3, 4, 5];
29+
let shared_data = AtomicConsume::new(data);
30+
31+
let mut join_handles = Vec::new();
32+
33+
for _ in 0..5 {
34+
let shared_data = shared_data.clone();
35+
36+
join_handles.push(thread::spawn(move || {
37+
let x = shared_data.load_consume();
38+
if let Some(v) = x {
39+
println!("Consumed: {}", v);
40+
}
41+
}));
42+
}
43+
44+
for handle in join_handles {
45+
handle.join().unwrap();
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crossbeam::atomic::AtomicCell;
2+
3+
pub fn atomic_cell_example() {
4+
let a = AtomicCell::new(0i32);
5+
6+
a.store(1);
7+
assert_eq!(a.load(), 1);
8+
9+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
10+
assert_eq!(a.fetch_add(1), 2);
11+
assert_eq!(a.load(), 3);
12+
assert_eq!(a.swap(100), 3);
13+
assert_eq!(a.load(), 100);
14+
assert_eq!(a.into_inner(), 100);
15+
16+
let a = AtomicCell::new(100i32);
17+
let v = a.take();
18+
assert_eq!(v, 100);
19+
assert_eq!(a.load(), 0);
20+
}
21+
22+
pub fn atomic_consume_example() {
23+
use crossbeam::atomic::AtomicConsume;
24+
25+
let data = vec![1, 2, 3, 4, 5];
26+
let shared_data = AtomicConsume::new(data);
27+
28+
let mut join_handles = Vec::new();
29+
30+
for _ in 0..5 {
31+
let shared_data = shared_data.clone();
32+
33+
join_handles.push(thread::spawn(move || {
34+
let x = shared_data.load_consume();
35+
if let Some(v) = x {
36+
println!("Consumed: {}", v);
37+
}
38+
}));
39+
}
40+
41+
for handle in join_handles {
42+
handle.join().unwrap();
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crossbeam::atomic::{AtomicCell,AtomicConsume};
2+
use std::thread;
3+
4+
pub fn atomic_cell_example() {
5+
let a = AtomicCell::new(0i32);
6+
7+
a.store(1);
8+
assert_eq!(a.load(), 1);
9+
10+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
11+
assert_eq!(a.fetch_add(1), 2);
12+
assert_eq!(a.load(), 3);
13+
assert_eq!(a.swap(100), 3);
14+
assert_eq!(a.load(), 100);
15+
assert_eq!(a.into_inner(), 100);
16+
17+
let a = AtomicCell::new(100i32);
18+
let v = a.take();
19+
assert_eq!(v, 100);
20+
assert_eq!(a.load(), 0);
21+
}
22+
23+
pub fn atomic_consume_example() {
24+
use crossbeam::atomic::AtomicConsume;
25+
26+
let data = vec![1, 2, 3, 4, 5];
27+
let shared_data = AtomicConsume::new(data);
28+
29+
let mut join_handles = Vec::new();
30+
31+
for _ in 0..5 {
32+
let shared_data = shared_data.clone();
33+
34+
join_handles.push(thread::spawn(move || {
35+
let x = shared_data.load_consume();
36+
if let Some(v) = x {
37+
println!("Consumed: {}", v);
38+
}
39+
}));
40+
}
41+
42+
for handle in join_handles {
43+
handle.join().unwrap();
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crossbeam::atomic::{AtomicCell,AtomicConsume};
2+
use std::thread;
3+
4+
pub fn atomic_cell_example() {
5+
let a = AtomicCell::new(0i32);
6+
7+
a.store(1);
8+
assert_eq!(a.load(), 1);
9+
10+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
11+
assert_eq!(a.fetch_add(1), 2);
12+
assert_eq!(a.load(), 3);
13+
assert_eq!(a.swap(100), 3);
14+
assert_eq!(a.load(), 100);
15+
assert_eq!(a.into_inner(), 100);
16+
17+
let a = AtomicCell::new(100i32);
18+
let v = a.take();
19+
assert_eq!(v, 100);
20+
assert_eq!(a.load(), 0);
21+
}
22+
23+
pub fn atomic_consume_example() {
24+
let data = vec![1, 2, 3, 4, 5];
25+
let shared_data = AtomicConsume::new(data);
26+
27+
let mut join_handles = Vec::new();
28+
29+
for _ in 0..5 {
30+
let shared_data = shared_data.clone();
31+
32+
join_handles.push(thread::spawn(move || {
33+
let x = shared_data.load_consume();
34+
if let Some(v) = x {
35+
println!("Consumed: {}", v);
36+
}
37+
}));
38+
}
39+
40+
for handle in join_handles {
41+
handle.join().unwrap();
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crossbeam::atomic::{AtomicCell,AtomicConsume};
2+
use std::thread;
3+
4+
pub fn atomic_cell_example() {
5+
let a = AtomicCell::new(0i32);
6+
7+
a.store(1);
8+
assert_eq!(a.load(), 1);
9+
10+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
11+
assert_eq!(a.fetch_add(1), 2);
12+
assert_eq!(a.load(), 3);
13+
assert_eq!(a.swap(100), 3);
14+
assert_eq!(a.load(), 100);
15+
assert_eq!(a.into_inner(), 100);
16+
17+
let a = AtomicCell::new(100i32);
18+
let v = a.take();
19+
assert_eq!(v, 100);
20+
assert_eq!(a.load(), 0);
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crossbeam::atomic::{AtomicCell,};
2+
3+
pub fn atomic_cell_example() {
4+
let a = AtomicCell::new(0i32);
5+
6+
a.store(1);
7+
assert_eq!(a.load(), 1);
8+
9+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
10+
assert_eq!(a.fetch_add(1), 2);
11+
assert_eq!(a.load(), 3);
12+
assert_eq!(a.swap(100), 3);
13+
assert_eq!(a.load(), 100);
14+
assert_eq!(a.into_inner(), 100);
15+
16+
let a = AtomicCell::new(100i32);
17+
let v = a.take();
18+
assert_eq!(v, 100);
19+
assert_eq!(a.load(), 0);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crossbeam::atomic::{AtomicCell};
2+
3+
pub fn atomic_cell_example() {
4+
let a = AtomicCell::new(0i32);
5+
6+
a.store(1);
7+
assert_eq!(a.load(), 1);
8+
9+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
10+
assert_eq!(a.fetch_add(1), 2);
11+
assert_eq!(a.load(), 3);
12+
assert_eq!(a.swap(100), 3);
13+
assert_eq!(a.load(), 100);
14+
assert_eq!(a.into_inner(), 100);
15+
16+
let a = AtomicCell::new(100i32);
17+
let v = a.take();
18+
assert_eq!(v, 100);
19+
assert_eq!(a.load(), 0);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crossbeam::atomic::AtomicCell;
2+
3+
pub fn atomic_cell_example() {
4+
let a = AtomicCell::new(0i32);
5+
6+
a.store(1);
7+
assert_eq!(a.load(), 1);
8+
9+
assert_eq!(a.compare_exchange(1, 2), Ok(1));
10+
assert_eq!(a.fetch_add(1), 2);
11+
assert_eq!(a.load(), 3);
12+
assert_eq!(a.swap(100), 3);
13+
assert_eq!(a.load(), 100);
14+
assert_eq!(a.into_inner(), 100);
15+
16+
let a = AtomicCell::new(100i32);
17+
let v = a.take();
18+
assert_eq!(v, 100);
19+
assert_eq!(a.load(), 0);
20+
}

0 commit comments

Comments
 (0)