Skip to main content

Crate qubit_atomic

Crate qubit_atomic 

Source
Expand description

§qubit-atomic

User-friendly atomic operations wrapper providing JDK-like atomic API.

This crate provides an easy-to-use generic atomic wrapper with reasonable default memory orderings, similar to Java’s java.util.concurrent.atomic package.

§Design Goals

  • Ease of Use: Hides memory ordering complexity with reasonable defaults
  • Completeness: Provides high-level operations similar to JDK atomic
  • Safety: Guarantees memory safety and thread safety
  • Performance: Zero-cost abstraction with no additional overhead
  • Flexibility: Exposes underlying types via inner() for advanced users

§Features

  • Primitive atomic values: Atomic<bool>, Atomic<i32>, Atomic<u64>, Atomic<usize>, Atomic<f32>, and other supported primitive types
  • Counter atomic types: AtomicCount, AtomicSignedCount
  • Reference atomic type: AtomicRef<T>

§Example

use qubit_atomic::Atomic;
use std::sync::Arc;
use std::thread;

// Basic usage
let counter = Atomic::new(0);
counter.fetch_inc();
assert_eq!(counter.load(), 1);

// Concurrent usage
let counter = Arc::new(Atomic::new(0));
let mut handles = vec![];

for _ in 0..10 {
    let counter = counter.clone();
    let handle = thread::spawn(move || {
        for _ in 0..100 {
            counter.fetch_inc();
        }
    });
    handles.push(handle);
}

for handle in handles {
    handle.join().unwrap();
}

assert_eq!(counter.load(), 1000);

§Author

Haixing Hu

Re-exports§

pub use atomic::Atomic;
pub use atomic::AtomicCount;
pub use atomic::AtomicRef;
pub use atomic::AtomicSignedCount;

Modules§

atomic
Atomic value types and reference/counting helpers. Atomic value wrappers, counters, and reference helpers.