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

<atomic>: std::atomic_ref<volatile T> compilation-errors #4719

Open
lewissbaker opened this issue Jun 9, 2024 · 4 comments
Open

<atomic>: std::atomic_ref<volatile T> compilation-errors #4719

lewissbaker opened this issue Jun 9, 2024 · 4 comments
Labels
blocked on LWG Waiting for WG21 to tell us what to do bug Something isn't working

Comments

@lewissbaker
Copy link

Describe the bug

The following code-snippet fails to compile under latest MSVC.

#include <atomic>

void test() {
    volatile int vi = 0;
    std::atomic_ref<volatile int> vref(vi);
    int val = vref.load(); // ok
    vref.exchange(val); // ok
    vref.fetch_add(0); // warning
    vref.fetch_sub(0); // warning
    vref.fetch_or(0); // warning
    vref.compare_exchange_weak(val, 0); // error
    vref.compare_exchange_strong(val, 0); // error
    vref.wait(0); // error
}

See https://godbolt.org/z/f5jP7zs5G

Expected behavior

I expected this code-snippet to compile successfully.

We should be able to use std::atomic_ref<volatile int> to read/write atomically to storage pointed to by a volatile int*. e.g. for storage mapped in via shared-memory.

STL version

Whatever is running in the current compiler-explorer under 'x64 msvc v19.latest'.
Diagnostic paths include '14.39.33321-Pre'

Additional context

Also potentially related - see Wg21 mailing list thread on potential specification issue relating to std::atomic_ref<volatile T>.
https://lists.isocpp.org/lib/2024/06/28451.php

Other standard libraries also seem to have similar issues:

@frederick-vs-ja
Copy link
Contributor

A volatile version of memcpy seems necessary for correct writing...

@pinskia
Copy link

pinskia commented Jun 9, 2024

https://cplusplus.github.io/LWG/issue3508

@StephanTLavavej StephanTLavavej added bug Something isn't working blocked on LWG Waiting for WG21 to tell us what to do labels Jun 12, 2024
@StephanTLavavej
Copy link
Member

Thanks @pinskia - this is definitely blocked on LWG-3508 that @CaseyCarter filed back in 2020. That needs to be resolved before we'll know what to do here.

@lewissbaker
Copy link
Author

The paper https://isocpp.org/files/papers/P3323R0.html proposes some improvements to std::atomic_ref to address some of the issues around use with cv-qualified types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked on LWG Waiting for WG21 to tell us what to do bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants