Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions wh/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions wh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "wh"
version = "0.1.0"
edition = "2024"

[dependencies]
rand = "0.9.1"
3 changes: 3 additions & 0 deletions wh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Atomic practice project

Based on: <https://www.bilibili.com/video/BV1ChTtznEJg>
28 changes: 28 additions & 0 deletions wh/src/bin/atomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{sync::atomic::AtomicUsize, thread, time::Instant};

static COUNTER: AtomicUsize = AtomicUsize::new(0);

fn main() {
let start = Instant::now();

let mut handles = Vec::new();

for _ in 0..1000 {
let h = thread::spawn(|| {
for _ in 0..1000 {
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
});
handles.push(h);
}

handles.into_iter().for_each(|h| h.join().unwrap());

println!(
"Total: {}",
COUNTER.load(std::sync::atomic::Ordering::Relaxed)
);

let elapsed = start.elapsed();
println!("Elapsed time: {}", elapsed.as_micros());
}
25 changes: 25 additions & 0 deletions wh/src/bin/static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{thread, time::Instant};

static mut COUNTER: usize = 0;

fn main() {
let start = Instant::now();

let mut handles = Vec::new();

for _ in 0..1000 {
let h = thread::spawn(|| {
for _ in 0..1000 {
unsafe { COUNTER += 1 }
}
});
handles.push(h);
}

handles.into_iter().for_each(|h| h.join().unwrap());

println!("Total: {}", unsafe { COUNTER });

let elapsed = start.elapsed();
println!("Elapsed time: {}", elapsed.as_micros());
}
56 changes: 56 additions & 0 deletions wh/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::sync::atomic::AtomicUsize;
use std::thread;
use std::time::Instant;

use rand::Rng;

fn main() {
let start = Instant::now();

// products, 1, 2, 3, 4, 5
let section_count = rand::rng().random_range(10..=20);

let mut sections = Vec::new();

let mut actual = [0; 5];

for _ in 0..section_count {
let mut section = Section([0; 5]);

for (i, p) in section.0.iter_mut().enumerate() {
*p = rand::rng().random_range(0..=1_000_000);
actual[i] += *p
}
sections.push(section);
}

println!("Actual: {actual:#?}");

let counted: [AtomicUsize; 5] = Default::default();

thread::scope(|s| {
for sec in sections.iter() {
s.spawn(|| {
for (i, c) in sec.0.iter().enumerate() {
for _ in 0..*c {
counted[i].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
}
});
}
});

println!("Counted: {counted:#?}");

for i in 0..5 {
assert_eq!(
actual[i],
counted[i].load(std::sync::atomic::Ordering::Relaxed)
)
}

let elapsed = start.elapsed();
println!("Elapsed time: {}", elapsed.as_micros());
}

struct Section([usize; 5]);