Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fetch_update never return #36

Closed
flisky opened this issue Jul 27, 2023 · 1 comment
Closed

bug: fetch_update never return #36

flisky opened this issue Jul 27, 2023 · 1 comment

Comments

@flisky
Copy link

flisky commented Jul 27, 2023

MRE:

use std::{
    sync::atomic::Ordering,
    time::{Duration, Instant},
};

#[test]
fn test() {
    let last_time = atomic::Atomic::new(Instant::now());
    let mut count = 0;
    loop {
        let update = last_time.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |old| {
            let now = std::time::Instant::now();
            (now.duration_since(old) >= Duration::from_millis(1)).then_some(now)
        });
        if update.is_ok() {
            println!("update: {update:?}"); // comment this line to see the difference
            count += 1;
            if count > 1 {
                break;
            }
        }
    }
}

It seems that accessing update will make the following compare_exchange_weak always failing. Please correct me if it's not a bug.


system: Darwin MacBook-Pro.local 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:19 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T8103 arm64
cargo: cargo 1.73.0-nightly (7ac9416d8 2023-07-24)

@Amanieu
Copy link
Owner

Amanieu commented Jul 29, 2023

Atomic<T> should only be used on types with no padding bytes. This is now enforced in 0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants