From f52db04807a70b9628e7154b331ed20916135f20 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 26 Jun 2025 16:11:24 +0800 Subject: [PATCH] finish atomic practice --- wh/Cargo.lock | 157 +++++++++++++++++++++++++++++++++++++++++++ wh/Cargo.toml | 7 ++ wh/README.md | 3 + wh/src/bin/atomic.rs | 28 ++++++++ wh/src/bin/static.rs | 25 +++++++ wh/src/main.rs | 56 +++++++++++++++ 6 files changed, 276 insertions(+) create mode 100644 wh/Cargo.lock create mode 100644 wh/Cargo.toml create mode 100644 wh/README.md create mode 100644 wh/src/bin/atomic.rs create mode 100644 wh/src/bin/static.rs create mode 100644 wh/src/main.rs diff --git a/wh/Cargo.lock b/wh/Cargo.lock new file mode 100644 index 0000000..8adc557 --- /dev/null +++ b/wh/Cargo.lock @@ -0,0 +1,157 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wh" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/wh/Cargo.toml b/wh/Cargo.toml new file mode 100644 index 0000000..7b4cf23 --- /dev/null +++ b/wh/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "wh" +version = "0.1.0" +edition = "2024" + +[dependencies] +rand = "0.9.1" diff --git a/wh/README.md b/wh/README.md new file mode 100644 index 0000000..dd575a2 --- /dev/null +++ b/wh/README.md @@ -0,0 +1,3 @@ +# Atomic practice project + +Based on: diff --git a/wh/src/bin/atomic.rs b/wh/src/bin/atomic.rs new file mode 100644 index 0000000..66e8f31 --- /dev/null +++ b/wh/src/bin/atomic.rs @@ -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()); +} diff --git a/wh/src/bin/static.rs b/wh/src/bin/static.rs new file mode 100644 index 0000000..fc08882 --- /dev/null +++ b/wh/src/bin/static.rs @@ -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()); +} diff --git a/wh/src/main.rs b/wh/src/main.rs new file mode 100644 index 0000000..8600e2d --- /dev/null +++ b/wh/src/main.rs @@ -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]);