volatile deprecations in c++20

Christian Franke Christian.Franke@t-online.de
Sat Oct 3 11:00:16 GMT 2026


On Mon, 28 Sep 2026 11:40:44 +0200, Christian Franke wrote:
> Jon Turney wrote:
>>
>> The remaining warnings with gcc 16 (caused by the change to 
>> -std=c++20 by default) relate to the deprecation of the 'volatile' 
>> qualifier, which fall into two groups:
>>
>> ...
>> I wonder if this is just wrong?
>
> The introduction of these errors IMO makes plenty of sense because 
> (volatile_var)++ "looks atomic" but never was. The 'volatile' only 
> ensures that memory accesses are not optimized away.
>
> With gcc 14, all the functions below translate to the same x86_64 code 
> if optimization is enabled:
>
> volatile unsigned i;
> void inc0() { ++i; }
> void inc1() { i++; }
> void inc2() { i = i + 1; }
> void inc3() { unsigned t = i; ++t; i = t; }
>
> x86_64 code:
>   movl    i(%rip), %eax
>   addl    $1, %eax
>   movl    %eax, i(%rip)
>
> Same without 'volatile':
>   addl    $1, i(%rip)

aarch64 code (after changing variable declaration to 'extern' to avoid 
usage of '.LANCHOR0'):
   adrp    x1, i
   ldr     w0, [x1, #:lo12:i]
   add     w0, w0, 1
   str     w0, [x1, #:lo12:i]

Without 'volatile': same code.


>
>
>>
>> Since the state of incyg is inspected in other threads to determine 
>> how to deliver a signal, marking these as 'volatile' might have been 
>> enough on single-processor systems, but perhaps we need a memory 
>> barrier here (as we already do in the assembly code which manipulates 
>> incyg), maybe by using InterlockedIncrement/Decrement()?
>
> Using 'incyg++' and 'incyg--' was possibly always not safe.
>
> InterlockedIncrement/Decrement() would help. These are #defined to 
> intrinsics _InterlockedIncrement/Decrement() which do the same as 
> __atomic_add/sub_fetch().
>
> #include <windows.h>
> unsigned i; // volatile not needed
>
> void inc0() { _InterlockedIncrement(&i); }
> void inc1() { __atomic_add_fetch(&i, 1, __ATOMIC_SEQ_CST); }
>
> x86_64 code:
>   lock addl    $1, i(%rip)

aarch64 code:
   adrp    x0, i
   mov     w1, 1
   add     x0, x0, :lo12:i
   ldaddal w1, w1, [x0]

There is no 'lock' prefix or 'ldadd*' in winsup/cygwin/scripts/gendef. 
The inc/decrements of 'incyg' are possibly not thread safe.

-- 
Regards,
Christian



More information about the Cygwin-developers mailing list