Questions tagged [memory-barriers]
A memory barrier is a special processor instruction that imposes restrictions on the order in which memory accesses become visible to other processors/cores in a multi-processor or multi-core system.
653
questions
1
vote
1answer
40 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;
...
2
votes
1answer
56 views
Request for review of memory orders of mpsc queue
I converted the C code of http://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue to C++.
At the time of writing this looks as follows:
struct MpscNode
{
std::...
1
vote
0answers
43 views
Why does GCC 10 mfence no more STD Atomic operations? Is it sufficient enough by the memory Model? [duplicate]
I discovered currently that GCC 10 will no more use the mov and mfence method and instead will use the implied lock by an xchg. Is this sufficient by the memory model to not break any stuff when using ...
0
votes
0answers
51 views
Understanding of memory barriers
Currently reading an article about memory barriers (in MESI) and I have few questions about that.
There are 4 types of memory barriers LoadLoad, StoreStore, LoadStore and StoreLoad.
I understand what ...
1
vote
0answers
23 views
Does cudaMemcpy from GPU to Persistent Memory requires flushing and fence operations aftwards?
I am doing cudaMemcpy operation from GPU to persistent memory. In the case of memcpy operation from DRAM to persistent memory additional flushing(clflush/clflushopt) and sfence operation is required ...
0
votes
0answers
54 views
semi-lock-free spsc queue with release-acquire memory ordering
Following is a simplified implementation of a semi-lock-free spsc queue.I intentionally simplified it to be easier to read and easier to reproduce the deadlock problem.
The problem is in the memory-...
7
votes
2answers
188 views
What are the exact inter-thread reordering constraints on mutex.lock() and .unlock() in c++11 and up?
According to https://en.cppreference.com/w/cpp/atomic/memory_order mutex.lock() and mutex.unlock() are acquire and release operations. An acquire operation makes it impossible to reorder later ...
0
votes
1answer
67 views
Is 11 a valid output under ISO c++ for x86_64,arm or other arch?
This question is based on Can't relaxed atomic fetch_add reorder with later loads on x86, like store can?
I agree with answer given.
On x86 00 will never occur because a.fetch_add has a lock ...
3
votes
3answers
161 views
Can't relaxed atomic fetch_add reorder with later loads on x86, like store can?
This program will sometimes print 00, but if I comment out a.store and b.store and uncomment a.fetch_add and b.fetch_add which does the exact same thing i.e both set the value of a=1,b=1 , I never get ...
-1
votes
0answers
24 views
All possible c++ memory ordering
I had questions like
Multithreading atomics a b printing 00 for memory_order_relaxed
output 10 with memory_order_seq_cst
Output 11 for this program never occurs
Now i want to run through all program ...
0
votes
1answer
58 views
Output 11 for this program never occurs
This time i use atomic_fetch_add . Here is how i can get ra1=1 and ra2=1 .
Both the threads see a.fetch_add(1,memory_order_relaxed); when a=0. The writes go into store buffer and isn't visible to the ...
0
votes
2answers
40 views
Multithreading atomics a b printing 00 for memory_order_relaxed
In the below code the write to a in foo is stored in store buffer and not visible to the ra in bar. Similarly the write to b in bar is not visible to rb in foo and they print 00.
// g++ -O2 -pthread ...
3
votes
1answer
53 views
Ordering of Intel non-temporal stores to the same cache line
Do non-temporal stores (such as movnti), to the same cache line, issued by the same thread, reach the memory in program order?
So that for a system with NVRAM (like Intel Cascade Lake processor with ...
0
votes
2answers
83 views
output 10 with memory_order_seq_cst
When i run this program i get output as 10 which seems to be impossible for me. I'm running this on x86_64 core i3 ubuntu.
If the output is 10, then 1 must have come from either c or d.
Also in thread ...
3
votes
1answer
50 views
Do atomic operations in Go make sure other variables are visible to other threads?
it make me confused, i reading golang memory model, https://golang.org/ref/mem
var l sync.Mutex
var a string
func f() {
a = "hello, world"
l.Unlock()
}
func main() {
l.Lock()
...
0
votes
0answers
71 views
Does atomic variable's access after mutex need memory order?
I have 2 threads, one atomic variable A and a mutex L:
thread 1:
void changeA(val)
{
// Will monotonically increase A
L.lock();
int oldVal = A.load(std::memory_order_relaxed);
A.store(max(...
0
votes
1answer
53 views
Barriers - how to be sure that a write operation on a register has been completed?
I need to initialize an interrupt controller (GICv3), ARM. To do it, I need to set some registers, but only after initializing register CTLR. I must be sure that CTLR register has been written ...
0
votes
1answer
52 views
Which types of memory_order should be used for non-blocking behaviour with an atomic_flag?
I'd like, instead of having my threads wait, doing nothing, for other threads to finish using data, to do something else in the meantime (like checking for input, or re-rendering the previous frame in ...
0
votes
0answers
65 views
Memory Barriers in Registered IO
I have use RIO + IOCP + MultiThreads + TCP.
Does RIODequeueCompletion() function make a memory barrier (acquire or full fence) and RIOSend/RIOReceive functions make a memory barrier (release or full ...
0
votes
0answers
12 views
How is memory barrier implemented in the mutex [duplicate]
I'm studying how a mutex is implemented. As far as I know, there are two key points:
atomic operation on a data (an integer);
memory barrier.
For example, I have a mutex named mtx.
mtx.lock();
a = 1;...
0
votes
0answers
38 views
Force release and acquire ordering for fetch and add with load/store
So I am trying to implement writer and reader methods in such a way where I always want the reader to read the value that was last written by the writer, and thus have reads ordered after writes. For ...
0
votes
0answers
36 views
Need we use some runtime memory fence mechanism comparing with asm volatile
Let's say we have such a piece of c++ code:
auto t1 = getSystemTime(); // Statement 1
foo(); // Statement 2
auto t2 = getSystemTime(); // Statement 3
auto elapsedTime = t2 - t1;
...
3
votes
1answer
66 views
Why “movnti” followed by an “sfence” guarantees persistent ordering?
SFENCE prevents NT stores from committing from the store buffer ahead of SFENCE itself.
NT store data enters an LFB directly from the store buffer.
Therefore SFENCE can only guarantees the ordering of ...
3
votes
1answer
95 views
PMC to count if software prefetch hit L1 cache
I am trying to find a PMC (Performance Monitoring Counter) that will display the amount of times that a prefetcht0 instruction hits L1 dcache (or misses).
icelake-client: Intel(R) Core(TM) i7-1065G7 ...
0
votes
0answers
52 views
Is memory reordering equivalent to instruction reordering? [duplicate]
I'm learning multi-thread programming.
But I'm confused with some words that "memory reordering" and "instruction reordering".
My question is that is "memory reordering" ...
2
votes
1answer
48 views
Behavior of breakpoint on relaxed memory models
Init: int x = y = 0;
thr1 thr2
---- ----
y = 1; x = 1; WRITE to global variables
a = x; b = y; READ from global variables
print(a); print(b);
In the above code snippet ...
2
votes
0answers
37 views
If stores are in order what's the point of sfence? [duplicate]
In AMD vol 2 (I'm reading 24593) 7.1.2 it says
Generally, out-of-order writes are not allowed. Write instructions executed out of order cannot commit (write) their result to memory until ...
8
votes
1answer
159 views
For purposes of ordering, is atomic read-modify-write one operation or two?
Consider an atomic read-modify-write operation such as x.exchange(..., std::memory_order_acq_rel). For purposes of ordering with respect to loads and stores to other objects, is this treated as:
a ...
2
votes
0answers
51 views
Does a relaxed memory-order spinlock always break synchronization?
Consider the following code:
int nonatom = 0;
std::atomic<int> atom{0};
// thread 1
nonatom = 1;
atom.store(1, std::memory_order_release);
// thread 2
while (atom.load(std::...
1
vote
2answers
82 views
What is the difference between memory barrier and complier-only fence
As the question stated, I'm confused about the difference between memory barrier and complier-only fence.
Are they the same? If not what is the difference between them?
0
votes
1answer
35 views
How do memory fences/barriers among threads interact with fences/barriers in other threads?
What is the interaction of memory fences in different threads?
More particularly does a memory fence in a thread only prevents the reordering of instructions within the thread or there is there ...
1
vote
0answers
81 views
choosing between relaxed and acquire/release memory ordering in atomics
I am implementing a very simple logic. Although, I am having a tough time figuring out the memory ordering to choose.
I have a simple struct like:
typedef unsigned int bitmap;
const size_t bits_count =...
2
votes
1answer
49 views
Why DMB barrier is neceesary even though d-cache is disabled
I was reading kernel source code (version, 5.9) and have a question in $(kernel_root)/arch/arm64/kernel/head.S.
There is a function SYM_FUNC_START_LOCAL(__create_page_tables) and some codes:
adr_l ...
3
votes
1answer
91 views
Is c++ singleton need memory barrier while using mutex?
I have known that mutex can also bring the effect as memory barrier from here: Can mutex replace memory barriers, but I always see there is an memory barrier using in c++ singleton example as below, ...
1
vote
0answers
47 views
C#: using volatile fields with Interlocked [duplicate]
My understanding is that all the Interlocked APIs in .NET will introduce a full memory fence. However, I still see many examples where volatile, which introduces half-fences, is used in conjunction ...
1
vote
0answers
48 views
Understanding the memory ordering for C++ atomics in a single thread
According to the point 1.10.19 in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf, the compiler cannot reorder the atomic operations on same object even in the relaxed ordering.
...
0
votes
1answer
74 views
C++ memory order for consistently storing in atomic variable from multiple threads
Running the following code hundreds of times, I expected the printed value to be always 3, but it seems to be 3 only about ~75% of the time. This probably means I have a misunderstanding about the ...
2
votes
1answer
66 views
is it possible to encourage threads to line up without forcing it?
I have a single atomic variable that multiple threads are loading, they perform some local calculations on it, then call an atomically fetch_and on it. They check that they were able to make there ...
2
votes
1answer
92 views
Some confused regarding to Rust memory order
I have some questions regarding to Rust memory barrier, let's have a look about this example, based on the example, I made some changes:
use std::cell::UnsafeCell;
use std::sync::atomic::{AtomicUsize, ...
1
vote
0answers
42 views
Why is StoreLoad more expensive than other barrier types?
There is already an question about why StoreLoad barrier is expensive, and the answer explained it is expensive because the StoreLoad barrier blocked the Load until the (potentially expensive) Store ...
2
votes
1answer
101 views
Why do read and write barrier for x86 in glibc not use __volatile asm?
I am studying glibc (The version is 2.32). As for memory barrier, read, write and full barrier for x86 are as follows:
#define atomic_full_barrier() \
__asm __volatile (LOCK_PREFIX "orl $0, (%...
0
votes
1answer
122 views
Memory barriers in async methods
This question is an extension of the answer to another question about memory barriers:
https://stackoverflow.com/a/3556877/13085654
Say you take that code example and tweak it to make the method async:...
0
votes
0answers
62 views
CPU prediction and memory barrier
I'm learning memory barrier so I referred to memory-barriers documentation in linux kernel source code. And there is one description that I can't understand:
Control dependencies can be a bit tricky ...
1
vote
1answer
126 views
Are memory barriers needed because of cpu out of order execution or because of cache consistency problem?
I'm wonderring why are memory barriers needed and I have read some articles about this toppic.
Someone says it's because of cpu out-of-order execution while others say it is because of cache ...
0
votes
0answers
80 views
Lock's semantic in intel architecture
the effect of Locked instruction will serialize all operation on multi-processor system ?
from the following description, seems P6 and more recent cpu promise this rule:
Locked operations are atomic ...
1
vote
1answer
36 views
What does it mean that “two store are seen in a consistent order by other processors”?
In intel's manual:
section of : "8.2.2 Memory Ordering in P6 and More Recent Processor Families"
Any two stores are seen in a consistent order by processors other than those performing the ...
4
votes
3answers
524 views
What is the difference between load/store relaxed atomic and normal variable?
As I see from a test-case: https://godbolt.org/z/K477q1
The generated assembly load/store atomic relaxed is the same as the normal variable: ldr and str
So, is there any difference between relaxed ...
1
vote
1answer
78 views
Understanding the Weak memory model
Assume we have two threads, working with two variables A and B in memory:
Thread 1 Thread 2
======== ========
1) A = 1 3) B = 1
2) Print(B) 4) Print(A)
I know in a Sequential ...
1
vote
2answers
155 views
C++ memory_order_acquire/release questions
I recently learn about c++ six memory orders, I felt very confusing about memory_order_acquire and memory_order_release, here is an example from cpp:
#include <thread>
#include <atomic>
#...
-1
votes
1answer
52 views
What is the impact of memory barriers against incrementing code?
Consider the following program: 2 threads are iterating through the same function that consists of incrementing the value of a shared counter variable. There's no lock protecting the variable, as such ...