705 questions
4 votes
1 answer
175 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 (...
10 votes
1 answer
300 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
183 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
346 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
190 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
123 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 ...
8 votes
1 answer
265 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
202 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
149 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
131 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
112 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
201 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
276 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, ...
5 votes
1 answer
148 views
Does release-consume order violate sequence-before order?
The following code is very easy illustrating the release-consume ordering: std::atomic<int> a{ 0 }; std::atomic<bool> b{ false }; void t1() { a.store(1, std::...
0 votes
2 answers
104 views
How to infer whether a global sequence is valid or not with std::memory_order_seq_cst?
#include <atomic> #include <thread> #include <cassert> #include <chrono> std::atomic<int> x {}, y {}; void write_x_and_y() { x.store(1, std::memory_order_seq_cst); ...