volatile deprecations in c++20
Christian Franke
Christian.Franke@t-online.de
Mon Sep 28 09:40:44 GMT 2026
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:
>
>> CXX dcrt0.o
>> ../../../../src/winsup/cygwin/dcrt0.cc: In function ‘void
>> dll_crt0_1(void*)’:
>> ../../../../src/winsup/cygwin/dcrt0.cc:805:11: error: ‘++’ expression
>> of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 805 | _my_tls.incyg++;
>> ../../../../src/winsup/cygwin/dcrt0.cc:981:15: error: ‘--’ expression
>> of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 981 | _my_tls.incyg--; /* Not in Cygwin anymore */
>> ../../../../src/winsup/cygwin/dcrt0.cc:1006:15: error: ‘--’
>> expression of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 1006 | _my_tls.incyg--; /* Not in Cygwin anymore */
>> CXX exceptions.o
>> ../../../../src/winsup/cygwin/exceptions.cc: In static member
>> function ‘static int exception::handle(EXCEPTION_RECORD*, void*,
>> CONTEXT*, PDISPATCHER_CONTEXT)’:
>> ../../../../src/winsup/cygwin/exceptions.cc:841:6: error: ‘++’
>> expression of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 841 | me.incyg++;
>> | ~~~^~~~~
>> ../../../../src/winsup/cygwin/exceptions.cc:856:6: error: ‘--’
>> expression of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 856 | me.incyg--;
>> | ~~~^~~~~
> > CXX signal.o> ../../../../src/winsup/cygwin/signal.cc: In
> function ‘void abort()’:
>> ../../../../src/winsup/cygwin/signal.cc:407:11: error: ‘++’
>> expression of ‘volatile’-qualified type is deprecated [-Werror=volatile]
>> 407 | _my_tls.incyg++;
>
> 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)
>
> 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)
>
>> CXX fork.o
>> ../../../../src/winsup/cygwin/fork.cc:47:40: error:
>> ‘volatile’-qualified parameter is deprecated [-Werror=volatile]
>> 47 | int parent (volatile char * volatile here);
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
>> ../../../../src/winsup/cygwin/fork.cc:48:39: error:
>> ‘volatile’-qualified parameter is deprecated [-Werror=volatile]
>> 48 | int child (volatile char * volatile here);
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
>> ../../../../src/winsup/cygwin/fork.cc:135:39: error:
>> ‘volatile’-qualified parameter is deprecated [-Werror=volatile]
>> 135 | frok::child (volatile char * volatile here)
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
>> ../../../../src/winsup/cygwin/fork.cc:208:40: error:
>> ‘volatile’-qualified parameter is deprecated [-Werror=volatile]
>> 208 | frok::parent (volatile char * volatile stack_here)
>> | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
>
> I can't see what these are doing at all (the parameter is unused in
> child(), and has the qualifier stripped off in the two places it's
> used in parent()), and the change which added them (in 2007) isn't
> very informative.
>
The 2nd 'volatile' could possibly be safely removed. It only applies to
the local variable containing the parameter value but not the function
signature. The following two prototypes refer to the same function:
int f(volatile char * volatile p);
int f(volatile char * p);
The 2nd 'volatile' would only be needed if the address of the parameter
variable would be passed to a thread. If this is really needed, it could
be handled with a copy to a volatile local variable.
--
Regards,
Christian
More information about the Cygwin-developers
mailing list