Re: using C11's _Atomic()

[email protected] (Leon Timmermans) Mon, 4 May 2026 02:25:34 +0200
Newsgroups perl.perl5.porters
Message-ID <CAHhgV8jUXqa2s2=BTdGXaXB+W644tnRm+zrYmuFv6gS24oSXew@mail.gmail.com>
On Mon, May 4, 2026 at 12:22 AM Tony Cook <[email protected]> wrote:

>
> Unfortunately C++ didn't add support for stdatomic.h until C++23[2] so
> any C++ XS modules that don't specify at least -std=c++23 may break.
>
> And C++ builds with g++ do break:
>
>  tony@venus:~/play$ cat iamatomic.c
>  #include <stdatomic.h>
>
>  _Atomic(int) count;
>
>  void
>  run_thread() {
>
>      count++;
>
>  }
>  tony@venus:~/play$ g++ -c -xc++ iamatomic.c
>  iamatomic.c:3:14: error: expected constructor, destructor, or type
> conversion before ‘count’
>      3 | _Atomic(int) count;
>        |              ^~~~~
>  iamatomic.c: In function ‘void run_thread()’:
>  iamatomic.c:8:5: error: ‘count’ was not declared in this scope
>      8 |     count++;
>        |     ^~~~~
>  tony@venus:~/play$ g++ -c -xc++ -std=c++20 iamatomic.c
>  iamatomic.c:3:14: error: expected constructor, destructor, or type
> conversion before ‘count’
>      3 | _Atomic(int) count;
>        |              ^~~~~
>  iamatomic.c: In function ‘void run_thread()’:
>  iamatomic.c:8:5: error: ‘count’ was not declared in this scope
>      8 |     count++;
>        |     ^~~~~
>  tony@venus:~/play$ g++ -c -xc++ -std=c++23 iamatomic.c
>  tony@venus:~/play$


That can be worked around by using std::atomic<int> instead of _Atomic(int)
in C++ mode (and including atomic).

Leon