709 questions
4
votes
3
answers
299
views
Does std::atomic remain atomic when a struct is allocated with malloc?
I have a struct that contains a std::atomic member:
#include <atomic>
#include <cstdint>
struct Foo {
std::atomic<int64_t> value;
int64_t age;
};
If I allocate memory for ...
4
votes
3
answers
228
views
Misbehaving circular ring buffer using std::atomic
The code below is a fully self-contained reproduction. It contains class called Racer which models a circular ring buffer. One thread tries to advance the head repeatedly, and another thread tries to ...
4
votes
1
answer
192
views
MSVC 14.38.33130: Does std::atomic_ref::is_lock_free have a bug? It returns true for a 1024-byte struct
I am encountering suspicious behavior with std::atomic_ref::is_lock_free() in Microsoft Visual Studio's C++ compiler (MSVC version 14.38.33130). The method returns true for a very large structure (...
11
votes
1
answer
322
views
How can you prevent a destructor call from being reordered above an atomic write by the compiler?
Here is a possibly incorrect program for communicating between a function and a signal handler that might interrupt it. Assume that HandleSignal has been arranged to run as a signal handler. The ...
4
votes
0
answers
196
views
Are writes (atomic or not) in a third thread visible due to a "happens before" relationship between two other threads?
As the title states, I have a question about the visibility of memory operation guarantees with happens before order. I've read the cppref memory order and a previous C++ iso draft, but I still cannot ...
6
votes
1
answer
371
views
Is it possible to build a mutex from C++20 std::atomic without spinning to avoid use-after-free when deleting?
Before I get to my main question, I'm assuming that std::mutex::unlock() stops touching this as soon as it puts the mutex in a state where another thread might lock it - even though the call to unlock(...
1
vote
1
answer
196
views
Is sequentially consistent memory ordering strictly necessary in this readers-writers lock using only load/store, not RMW?
Consider this outline of a simple multi-threaded application. It has one writer thread, and ten reader threads.
#include <atomic>
#include <thread>
const int Num_readers{...
3
votes
1
answer
130
views
Is it thread-safe to use std::atomic<bool> to control access to a data structure cooperatively, with just loads/stores instead of RMW?
I'm playing around with lockless C++ programming, and I came up with a method for safely(?) transferring ownership of a non-thread-safe data structure (std::unordered_map<int, float> in my ...
9
votes
1
answer
294
views
Valgrind (Helgrind) is showing data race with atomic
In the following program, I want to print the output as
0102030405... so on
The 0 should be printed from one thread, odd values should be printed from second thread and even values from third thread....
5
votes
0
answers
215
views
Why is std::atomic<T> larger than T itself for user-defined structs on MSVC but not on GCC/Clang?
I was checking the size of std::atomic compared to T on different platforms (Windows/MSVC, Linux/GCC, Android/Clang).
For intrinsic types (like int, int64_t, etc.), the size of std::atomic matches the ...
1
vote
1
answer
155
views
If `std::atomic_thread_fence(std::memory_order_acquire);` doesn't have an "associated atomic operation"... how does the fence gets anchored, to what?
An acquire-like load... will keep everything (both stores and loads) BELOW the load/fence.
But this doesn't mean that everything ABOVE/before the acquire-load will not move below...
This means that ...
2
votes
1
answer
149
views
Is it possible to use non-paired Acquire/Release memory orders?
I’ve spent several hours studying memory orderings, but I still have some contradictions in my head. One of them concerns the Acquire/Release memory orders.
Currently, my understanding is:
No ...
1
vote
0
answers
120
views
How to Portably Use std::atomic Inside a Union Across Platforms (MSVC/Clang on Windows/macOS/Linux)?
I'm working on a cross-platform data structure and trying to define a compact union-based layout that allows atomic access to a 64-bit word, while also optionally accessing the lower 32-bit fields.
I ...
1
vote
1
answer
268
views
Cross-platform 128-bit atomic support: std::atomic vs std::atomic_ref on Clang/MSVC (macOS ARM64, Windows x64, Linux)
Background
I'm building a cross-platform atomic abstraction layer to support 64-bit and 128-bit atomic operations for the following types:
int64_t, uint64_t
__int128 (on Clang platforms)
A custom ...
7
votes
0
answers
320
views
C++ memory order relaxed for a SeqLock producer and consumer algorithm
below is a copy screen from https://www.youtube.com/watch?v=5uIsadq-nyk, at 1:12:23, the line std::size_t seq2=seq.load(std::memory_order_relaxed) baffles me. I am not very familiar with the atomics, ...