Skip to content

Make the cached hardware support atomic in the C++ build too - #950

Merged
lemire merged 2 commits into
masterfrom
fix-hardware-support-race-cpp11
Nov 3, 2026
Merged

lemire merged 2 commits into
masterfrom
fix-hardware-support-race-cpp11

Conversation

@lemire

@lemire lemire commented Nov 3, 2026

Copy link
Copy Markdown
Member

croaring_hardware_support() caches the detected CPU features in a function-local static int. It is _Atomic only for the C11 atomic implementation, so compiling the sources as C++ leaves a data race (reported by ThreadSanitizer).

This takes the approach of #949 (a croaring_atomic_int typedef: std::atomic<int> in C++, _Atomic int in C11, plain int otherwise) and fixes its C++11/14 build: std::atomic<int> x = 0xFFFFFFF; is copy-initialization, which needs the deleted copy constructor before C++17. The initializer is now a macro that expands to {0xFFFFFFF} in C++ (copy-list-initialization, which calls the constexpr constructor directly) and to 0xFFFFFFF in C (clang rejects braces around an _Atomic initializer).

Compilers without atomics keep the plain int, as before.

Checked with -Wall -Wextra -Werror: C++11/14/17/20 (clang, g++-15), C11 (clang, gcc-15), and C99 with CROARING_ATOMIC_IMPL=CROARING_ATOMIC_IMPL_NONE, on arm64, x86_64 and x86_64 with AVX2.

Supersedes #949. Thanks to @moshap-firebolt for the diagnosis and the original fix.

croaring_hardware_support() caches the detected CPU features in a
function-local static int that was _Atomic only for the C11 atomic
implementation, leaving a data race when the sources are compiled as
C++. Use std::atomic<int> in C++, initialized with braces so that it
compiles before C++17 (std::atomic has no copy constructor).

Based on #949.

Co-authored-by: moshap <moshap@firebolt.io>
@lemire
lemire merged commit e201c6c into master Nov 3, 2026
43 checks passed
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

Successfully merging this pull request may close these issues.

1 participant