pub struct AtomicBool { /* private fields */ }Expand description
A boolean type which can be safely shared between threads.
This type has the same size, alignment, and bit validity as a bool.
Note: This type is only available on platforms that support atomic
loads and stores of u8.
Implementations§
Source§impl AtomicBool
impl AtomicBool
1.0.0 (const: 1.24.0) · Sourcepub const fn new(v: bool) -> AtomicBool
pub const fn new(v: bool) -> AtomicBool
Creates a new AtomicBool.
§Examples
1.0.0 · Sourcepub fn load(&self, order: Ordering) -> bool
pub fn load(&self, order: Ordering) -> bool
1.0.0 · Sourcepub fn store(&self, val: bool, order: Ordering)
pub fn store(&self, val: bool, order: Ordering)
1.0.0 · Sourcepub fn swap(&self, val: bool, order: Ordering) -> bool
pub fn swap(&self, val: bool, order: Ordering) -> bool
Stores a value into the bool, returning the previous value.
swap takes an Ordering argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
Acquire makes the store part of this operation Relaxed, and
using Release makes the load part Relaxed.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
1.10.0 · Sourcepub fn compare_exchange(
&self,
current: bool,
new: bool,
success: Ordering,
failure: Ordering,
) -> Result<bool, bool>
pub fn compare_exchange( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>
Stores a value into the bool if the current value is the same as the current value.
The return value is a result indicating whether the new value was written and containing
the previous value. On success this value is guaranteed to be equal to current.
compare_exchange takes two Ordering arguments to describe the memory
ordering of this operation. success describes the required ordering for the
read-modify-write operation that takes place if the comparison with current succeeds.
failure describes the required ordering for the load operation that takes place when
the comparison fails. Using Acquire as success ordering makes the store part
of this operation Relaxed, and using Release makes the successful load
Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use std::sync::atomic::{AtomicBool, Ordering};
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.compare_exchange(true,
false,
Ordering::Acquire,
Ordering::Relaxed),
Ok(true));
assert_eq!(some_bool.load(Ordering::Relaxed), false);
assert_eq!(some_bool.compare_exchange(true, true,
Ordering::SeqCst,
Ordering::Acquire),
Err(false));
assert_eq!(some_bool.load(Ordering::Relaxed), false);§Considerations
compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides
of CAS operations. In particular, a load of the value followed by a successful
compare_exchange with the previous load does not ensure that other threads have not
changed the value in the interim. This is usually important when the equality check in
the compare_exchange is being used to check the identity of a value, but equality
does not necessarily imply identity. In this case, compare_exchange can lead to the
ABA problem.
1.0.0 · Sourcepub fn fetch_and(&self, val: bool, order: Ordering) -> bool
pub fn fetch_and(&self, val: bool, order: Ordering) -> bool
Logical “and” with a boolean value.
Performs a logical “and” operation on the current value and the argument val, and sets
the new value to the result.
Returns the previous value.
fetch_and takes an Ordering argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
Acquire makes the store part of this operation Relaxed, and
using Release makes the load part Relaxed.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use std::sync::atomic::{AtomicBool, Ordering};
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), false);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(true, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_and(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), false);1.0.0 · Sourcepub fn fetch_or(&self, val: bool, order: Ordering) -> bool
pub fn fetch_or(&self, val: bool, order: Ordering) -> bool
Logical “or” with a boolean value.
Performs a logical “or” operation on the current value and the argument val, and sets the
new value to the result.
Returns the previous value.
fetch_or takes an Ordering argument which describes the memory ordering
of this operation. All ordering modes are possible. Note that using
Acquire makes the store part of this operation Relaxed, and
using Release makes the load part Relaxed.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use std::sync::atomic::{AtomicBool, Ordering};
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_or(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_or(true, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_or(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), false);