Skip to main content

Crate atomic_bitflags

Crate atomic_bitflags 

Source
Expand description

Atomic storage for a bitflags flag set.

AtomicFlags<F> keeps the bits of a bitflags type in an atomic integer, so a flag set can be shared between threads without a lock.

Every operation on the stored value takes an Ordering under the same rules as the standard library atomics. Flags wider than 64 bits are not supported, and 64-bit atomics are not available on every target.

§Examples

use atomic_bitflags::AtomicFlags;
use bitflags::bitflags;
use std::sync::Arc;
use std::sync::atomic::Ordering;

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct Features: u8 {
        const FAST = 0b001;
        const SAFE = 0b010;
    }
}

let flags = Arc::new(AtomicFlags::new(Features::FAST));
let remote = Arc::clone(&flags);

std::thread::spawn(move || {
    remote.set(Features::SAFE, Ordering::Relaxed);
})
.join()
.unwrap();

let current = flags.load(Ordering::Relaxed);
assert!(current.contains(Features::FAST | Features::SAFE));

Structs§

AtomicFlags
A flag set stored in an atomic integer.

Traits§

AtomicBits
Integer type that can back an AtomicFlags.