45 questions
3
votes
2
answers
163
views
Why std::atomic::store can be moved ahead of its argument's initialization in runtime?
I'm reading this: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3292r0.html
Here's code sample the question is about:
int dummy;
std::atomic<int *> ptr_to_int_1{&dummy};
std::...
0
votes
0
answers
72
views
Can the OoO scheduler squash pending instructions (issued by earlier validations) based on predictable behavior?
In the idiom:
while (exp == V.loadRelaxed()) { // allows the early issuing of any n-loads that'd fit within the speculative window.
if (V.weakRMWRelease(exp, set)) return true; // ^^ (upward ...
0
votes
0
answers
53
views
Does `memory_order_relax` loads prevents cross `JMP` reorderings at the backedge of loops during prediction window OoO scheduling?
I was wondering about what the consequences of a relaxed load in a while loop would be, and if it would be sufficient fencing.
My question arises from the fact that the Java community has agreed that &...
1
vote
0
answers
88
views
std::thread::join call reordering [duplicate]
I have a thread pool implementation, which stops all threads by setting a special atomic bool stop to true. Its destructor looks like this:
ThreadPool::~ThreadPool()
{
stop.store(true);
for (...
0
votes
2
answers
227
views
Why is an acquire barrier cannot stop a reordering around a branch?
I was testing the behavior of the control dependencies in LINUX KERNEL MEMORY BARRIERS, and had a problem with the location of the fence.
I was testing this on AArch64 on a Qualcomm Snapdragon 835, ...
1
vote
1
answer
92
views
Does Volatile.Read() still apply if it is nested in a getter?
Consider this code in C#:
private int _backingField;
public int SomeNumber => Volatile.Read(ref _backingField);
public void Test_1()
{
var someNumber = Volatile.Read(ref _backingField);
/...
0
votes
0
answers
23
views
JMM reordering and cache sync memory
public class Cache_01_Sample {
@JCStressTest
@Outcome(expect = ACCEPTABLE, desc = "9")
@Outcome(id = "-1", expect = FORBIDDEN, desc = "0")
@State
...
1
vote
0
answers
96
views
How to ensure memory consistency with memcpy in a seq_lock implementation for reader/writer threads?
I am using a seq_lock for coordination between a reader and a writer thread. The reader calls load(), and the writer calls store(). Here's a simplified version of my code:
#include <atomic>
#...
-1
votes
2
answers
134
views
Will an acquire operation prevent reordering if there's no corresponding release?
I'm working with atomic operations in a multi-threaded program in C++ and trying to understand how acquire semantics behave when used without a corresponding release. Specifically, I know that acquire ...
1
vote
1
answer
343
views
Relaxed Memory ordering on fetch_add
I'm going through Mara's Atomics and locks book, which shows this example code where it assumes that the function a and b are executed concurrently:
static X: AtomicI32 = AtomicI32::new(0);
fn a() {
...
1
vote
0
answers
107
views
Connection between reordering and memory barrier
I thought typically memory barriers can be categorized into four types.
Some of the barriers include multiple functions.
For example, StoreLoad can both prevent reordering between store and load, and ...
0
votes
0
answers
142
views
prevent instruction reordering when profiling a piece of code
Is there a standard way to prevent code reordering when profiling functions in c++? Let's say I have a code like this
void foo() {
const auto timeStart = std::chrono::system_clock::now();
// ...
0
votes
0
answers
17
views
C - possible reordering issue with pipe access and mutex
I have 2 threads running after write_fd was initialized by a call to pipe():
static bool finished = false;
Thread 1
pthread_mutex_lock(&lock);
if (!finished) {
write(write_fd, buf, buf_len);
}...
0
votes
0
answers
129
views
x86: Instruction reorder related with `cmpxchg` (without lock prefix)?
Could a memory write instruction (mov reg to memory) after cmpxchg (without lock prefix) be recordered and executed before cmpxchg for x86?
EX1:
// try_cmpxchg_local(&local.pos, &tail, ...
2
votes
1
answer
126
views
About c++ memory order: how to keep other threads to access common resources safely?
This is my code: Godbolt.
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
int main(int, char **) {
volatile bool resource = true;
std::...
1
vote
2
answers
458
views
Why does this C++ code crash with an apparent memory ordering race condition?
Why does this kind of code crash (very occasionally!) on x64?
class Object
{
public:
void manipulate(); // reads and writes object
bool m_finished = false; // note this is a regular bool and ...
1
vote
1
answer
441
views
Who actually does the out of ordering of the memory accesses in MPCore?
As per my current understanding from the ARM Cortex A57 and A78 TRM, micro ops can be issued out of order to 1 among the several execution pipelines.
This is instruction reordering for independent ...
1
vote
1
answer
419
views
Why debug execution order doesn't match code order in c++?
I am new to C++. When I debugged in Clion, I found that the execution order using Step over (F8) doesn't match the real code's order. So far, I think the most possible reason is compiler optimization. ...
10
votes
1
answer
663
views
Optimizations around atomic load stores in C++
I have read about std::memory_order in C++ and understood partially. But I still had some doubts around it.
Explanation on std::memory_order_acquire says that, no reads or writes in the current ...
0
votes
0
answers
32
views
Why do stores have to wait until they are committed before they modify memory? [duplicate]
In the context of out of order and dynamic scheduling in the topic of computer architecture, why do store instructions have to wait until they are committed (retired) before they modify memory?
I ...
1
vote
2
answers
102
views
Does the C# compiler reorder File-IO instructions?
I have the following C# algorithm for config file writeback:
string strPathConfigFile = "C:\File.txt"
string strPathBackupFile = "C:\File.backup"
string strContent = "File ...
1
vote
0
answers
161
views
How to guarantee ordering of instruction in C
My multithreaded code is as follows:
thread A
while(true) {
foo();
sleep();
}
thread B
...
bar_off();
bar_on();
...
foo() isn't safe to be executed when bar_off is called and until bar_on has ...
5
votes
1
answer
690
views
Is using std::atomic_thread_fence right before an atomic load/store with the same order always redundant?
Given:
std::atomic<uint64_t> b;
void f()
{
std::atomic_thread_fence(std::memory_order::memory_order_acquire);
uint64_t a = b.load(std::memory_order::memory_order_acquire);
// code ...
5
votes
1
answer
562
views
Does memory_order_relaxed respect data dependencies within the same thread?
Given:
std::atomic<uint64_t> x;
uint64_t f()
{
x.store(20, std::memory_order::memory_order_relaxed);
x.store(10, std::memory_order::memory_order_relaxed);
return x.load(std::...
1
vote
2
answers
113
views
Can "b=3" be reordered before "a=2+b" if a is volatile and b is not?
Because ordinary reading and writing after volatile writing does not prohibit reordering, may b=3 in the following code be reordered before a=2+b?
volatile int a = 1;
int b = 2;
private void demo () {...
1
vote
1
answer
700
views
Java instruction reordering and CPU memory reordering
This is a follow up question to
How to demonstrate Java instruction reordering problems?
There are many articles and blogs referring to Java and JVM instruction reordering which may lead to counter-...
0
votes
1
answer
180
views
Why does multithreaded code using CancellationTokenSource.Cancel require less anti-reordering measures
I'm trying to understand why a shared CancellationTokenSource variable is not protected by a lock or memory barriers here.
I know there is a rule of thumb that a read or a write of a shared (state) ...
6
votes
1
answer
2k
views
How does Google's `DoNotOptimize()` function enforce statement ordering
I'm trying to understand exactly how Google's DoNotOptimize() is supposed to work.
For completeness, here is its definition (for clang, and non-const data):
template <class Tp>
inline ...
0
votes
1
answer
2k
views
Difference between data dependence and control dependence
I'm struggling to understand the difference between data dependence and control dependence .
So what I saw as an example was :
data dependence e.g., instruction uses data created by another ...
0
votes
1
answer
263
views
Compiler reordering section with mutual exclusion
Since rust applies the mutex as a container/owner of its data and doesn't use an external guard like C++, I was wondering whether the rust compiler might reorder the internal part of the loop in the ...
1
vote
1
answer
217
views
When should I think about Memory Barrier and Instruction Reorder?
I tried to implement Peterson Lock with C# like this
public class PetersonLock
{
private volatile bool[] flag = new bool[2];
private volatile int victim;
public int oneThreadId;
...
1
vote
0
answers
199
views
Is this execution reordering possible
As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code:
C = A + B;
D = E + F;
The compiler or the ...
1
vote
1
answer
152
views
Does as-if rule allow this kind of execution reordering
As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code:
C = A + B;
D = E + F;
The compiler or the ...
2
votes
0
answers
112
views
Is the compiler allowed to reorder statements around `std::condition_variable::notify_one`?
The following code is a struct I have written to allow me to queue up work to run on multiple worker threads, and which blocks the main thread until a new worker thread is available.
struct WorkQueue{
...
3
votes
2
answers
226
views
Can object access be reordered with that object's final field access in Java?
Below code sample is taken from JLS 17.5 "final Field Semantics":
class FinalFieldExample {
final int x;
int y;
static FinalFieldExample f;
public FinalFieldExample() {
x = ...
3
votes
3
answers
1k
views
memory model, how load acquire semantic actually works?
From very nice Paper and article about memory reordering.
Q1: I understand that cache-coherence, store buffer and invalidation queue is root cause of memory reordering ?
Store release is quite ...
0
votes
2
answers
433
views
Does StoreStore memory barrier in Java forbid the read-write reordering?
Now we have
Load A
StoreStore
Store B
Is it possible that the actual execution order is as follows
StoreStore
Store B
Load A
If it is possible, how to explain a situation which seems to violate the ...
1
vote
2
answers
398
views
Do we have the guarantee that any atomic write will store the new value of the atomic variable in the main memory immediately?
So, I was reading a lot about instruction and memory reordering and how we can prevent it, but i still have no answer to one qustion (probably because I'm not attentive enough). My question is: Do we ...
1
vote
1
answer
80
views
Sort array values by specific order given in another array
I have this array that is the correct order:
$orderDoc = array("ORT", "TRI", "CONT", "RMI");
These documents are in different tables of several servers and database.
At the end of doing the search of ...
1
vote
1
answer
202
views
How can I produce instruction reordering in jcstress test
When I am suspicious certain instruction reordering is allowed by the java language specification, I want to reproduce it in a jcstress test. How can I do that? For example, in the following code, the ...
2
votes
1
answer
106
views
Interlocked.CompareExchange instruction reodering of the initialvalue
Iam wondering if its possible that the initialvalue in the following code can be reordered to be after the computation resulting in undefined behavior.
The following example is taken from https://...
0
votes
1
answer
99
views
How compiler and processor optimization works to speed up code execution?
In Linux Kernel Development (3rd Edition), Kernel Synchronization Methods, Ordering and Barriers. There is an example:
And I'm confused about the statement in this book:
Again, without memory ...
6
votes
1
answer
2k
views
What's the relation between instruction reordering done by a compiler and instruction reordering done by a cpu?
OK, so a compiler is free to reorder code fragments for performance reasons. Let's suppose some code snippet, translated directly into machine code with no optimizations applied, looks like this:
...
46
votes
3
answers
3k
views
How to demonstrate Java instruction reordering problems?
With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order.
Edit: [...
7
votes
3
answers
549
views
sequenced-before modification order consistency
from http://en.cppreference.com :
Relaxed ordering Atomic operations tagged std::memory_order_relaxed are not synchronization operations, they do not order memory. They only guarantee atomicity and ...