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.

memory-barriers
Filter by
Sorted by
Tagged with
1 vote
4 answers
783 views

Why does this acquire and release memory fence not give a consistent value?

im just exploring the use of acquire and release memory fences and dont understand why i get the value output to zero sometimes and not the value of 2 all the time I ran the program a number of ...
6 votes
2 answers
589 views

Understanding sequential consistency fence in C++

I was reading about memory orders in C++. I could understand relaxed and acquire-release models a well. But I'm struggling with sequential-consistency. If I am not wrong, from cppreference, std::...
8 votes
3 answers
167 views

Will memory write be visible after sending an IPI on x86?

I have read Intel 64 and IA-32 Architectures SDM vol 3A, 9.2 MEMORY ORDERING, but there was one question that kept bothering me. If I first write to a memory address, then send an interprocessor ...
1 vote
2 answers
68 views

Unexpected inter-thread happens-before relationships from relaxed memory ordering

I'm working my way through C++ concurrency in action and ran into a problem trying to understand listing 5.12, reproduced below (GitHub code sample). I understand why the following should work when ...
0 votes
0 answers
26 views

Cache line management and memory ordering

I'm trying to understand how memory ordering affects to the number of writes to memory across different architectures. Consider the following code snippet: STR 0, [addr,0] STR 1, [addr,1] STR 2, [addr,...
0 votes
1 answer
66 views

Replicating a race condition with memory_order_relaxed

void experiment_relaxed() { atomic<int> x; atomic<int> y; auto write = [&x, &y]() { y.store(10, memory_order_relaxed); x.store(1, memory_order_relaxed); }; ...
0 votes
0 answers
90 views

Is `std::memory_order` a special tag for compilers at the compile time?

The arguments of type std::memory_order passed to atomic operation affect compile-time and runtime. However, I wonder if this type is a special tag for the compiler. In other words, when given the ...
2 votes
2 answers
802 views

Memory Barrier Vs CAS

I find that CAS will flush all CPU write cache to main memory。 Is this similar to memory barrier? If this is true, does this mean CAS can make java Happens-Before work? For answer: The CAS is CPU ...
2 votes
0 answers
88 views

Prevent reordering of prefetch instruction in c++

Usecase: I have an SPSC queue in multi-thread setup, where I want to prefetch the write_index's mempool on a successful pop. Following is my original implementation: void process() { if(spsc_queue-&...
0 votes
1 answer
291 views

Why a DSB SY is required after updating ARM gic ICC_PMR register?

In the linux kernel, updating SYS_ICC_PMR_EL1 is followed with a DSB SY if ICC_CTLR_EL1.PMHE == 0b1. ./arch/arm64/include/asm/irqflags.h: 45 write_sysreg_s(GIC_PRIO_IRQON, SYS_ICC_PMR_EL1)...
1 vote
1 answer
57 views

About memory barrier

In a NUMA multi-CPU architecture, the initial value of a is 0 and it is in a shared state between CPU-x and CPU-y. At time t0, CPU-x executes a = 1 followed immediately by an smp_wmb, and then at a ...
3 votes
0 answers
133 views

How CPUs Use the LOCK Prefix to Implement Cache Locking and ensure memory consistency

In Java, adding the volatile keyword to a variable guarantees memory consistency (or visibility). On the x86 platform, the Hotspot virtual machine implements volatile variable memory consistency by ...
1 vote
2 answers
92 views

memory order with multiple stores

Consider the example below. Assume that barrier is initialized to 0. There is one producer thread and two consumer threads that constantly check barrier. If the barrier is set, they decrease runcnt. ...
4 votes
1 answer
114 views

Is fail ordering relevant for x86 atomic operation?

Consider the definition of compare_and_exchange_strong_explicit: _Bool atomic_compare_exchange_strong_explicit( volatile A* obj, C* expected, C desired, ...
0 votes
1 answer
111 views

Why std::mutex of c++11 has no memory order?

I mean compared with c++11 atomic, for example: #include <iostream> #include <thread> #include <atomic> std::atomic<int> counter(0); void incrementCounter() { for (int i =...
1 vote
1 answer
86 views

Why does a load-load control dependency require a full read memory barrier

Why exactly is a full read memory barrier required in the kernel docs at Documentation/memory-barriers.txt:709: q = READ_ONCE(a); if (q) { <read barrier> // why? p = READ_ONCE(b); } The ...
2 votes
2 answers
592 views

Why doesn't the instruction reorder issue occur on a single CPU core?

From this post: Two threads being timesliced on a single CPU core won't run into a reordering problem. A single core always knows about its own reordering and will properly resolve all its own memory ...
0 votes
1 answer
110 views

Is it guaranteed, that read-modify-write operation reads (and returns) a correct old value on weak memory models?

Let's say, I have a following code, that utilizes RMW operations, and is executed on WMM CPU (for example, on ARM): std::atomic<int> shared; std::atomic<bool> t0_is_last; std::atomic<...
0 votes
2 answers
81 views

Why does std::memory_order_acq_rel always trigger warnings in C++11?

My compiler is clang 18.1.0-rc1; and the following code triggers two warnings: #include <atomic> std::atomic<int> n; int main() { // Warning: Memory order argument to atomic ...
2 votes
2 answers
87 views

How does this acquire-release relationship work?

In Rust Atomics and Locks, more or less the following code is suggested for appropriately implementing the drop trait of a simplified Arc: (code is mine) unsafe { if 1 == (*self....
0 votes
0 answers
65 views

Relaxed ordering modern C++

I am going thought the C++ 11 memory model/ordering standard for revision purposes . For std::memory_order::relaxed there is the following example // Thread 1: r1 = y.load(std::memory_order_relaxed);...
3 votes
2 answers
344 views

Relation of Mutex and CPU caches (and memory fences)

Suppose I have an application with multiple threads that need to access some shared data. I know that a mutex (Critical Section) can be used to ensure that at most one thread at a time can access the ...
0 votes
1 answer
135 views

Linux kernel memory model pointer access and dereference

I was reading through the guide to using the various memory barriers provided by Linux and came upon the example below. I was curious as to the reason why CPU2 will load P into Q before issuing the ...
2 votes
1 answer
194 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 ...
0 votes
0 answers
24 views

bounded lock free mcsc queue read invalid value

multi threads call push(1), single thread call pop, why pop return true, but out value is 0? ingore queue size, push(1) times is less than queue SIZE already add memory fence, between store data and ...
2 votes
1 answer
110 views

memory_order: acquire/release reversed pattern

Might be stupid but rather being asked than regret for the rest of my life. The standard pattern of std::atomic::load and std::atomic::store would be something like this N.B.: assume that in all ...
0 votes
0 answers
76 views

acquire/release memory order on different processors

Below is from cppreference describing the memory order. It looks like acquire/relase will do two things here: Prevent (some) reordering within a thread Make acquire load after release store (Enforce ...
0 votes
1 answer
114 views

How `memory_order_relaxed` is enough in TTAS spinlock for Arm64?

Consider the following implementation of spinlock (first link in google on query "c++ spinlock implementation"): struct spinlock { std::atomic<bool> lock_ = {0}; void lock() noexcept {...
2 votes
0 answers
57 views

Invalidation of an Exclusive cache line

What happens if a CPU receives an invalidation message for a cache line in the Exclusive state? Can this message enter the invalidation queue? If so, what happens if the same CPU attempts to to that ...
2 votes
0 answers
37 views

In the implemention of urcu-qsbr, is there any mechanism to ensure the thread offline/online is visible to writer-thread?

In the implementation of urcu-qsbr, users can use urcu_qsbr_thread_online and urcu_qsbr_thread_offline to mark a critical section for reading. Here's offline/online Writers then use ...
2 votes
2 answers
181 views

what does the _mm_mfence() function do

Looking into the Intel Intrinsics documentation, the synopsis for _mm_mfence is as follows Perform a serializing operation on all load-from-memory and store-to-memory instructions that were issued ...
37 votes
2 answers
21k views

Atomicity of loads and stores on x86

8.1.2 Bus Locking Intel 64 and IA-32 processors provide a LOCK# signal that is asserted automatically during certain critical memory operations to lock the system bus or equivalent link. While this ...
4 votes
1 answer
183 views

Re-ordering between two atomic loads

I have the following program: int normalData[2]; std::atomic<int> counter {0}; // thread A: // write new data normalData[(counter + 1) % 2] = newData; counter.fetch_and_add(1, std::...
0 votes
0 answers
121 views

Memory Synchronization with callback called from a different thread

This is a follow-up question to my previous one I have two threads I wish to synchronize using atomics, and with, say, the most lenient memory order possible... //class members std::atomic_bool ...
0 votes
1 answer
85 views

arm gcc: store-store ordering without volatile?

I am trying to use a shared index to indicate that data has been written to a shared circular buffer. Is there an efficient way to do this on ARM (arm gcc 9.3.1 for cortex M4 with -O3) without using ...
0 votes
0 answers
69 views

Does getOpaque/order_relaxed/read_once have influence on the processor, or just the compiler during memory hoisting?

I've had some discussion with multiple people on this issue, and there are some points that makes the usage of memory ordering fences on load situations somewhat confusing. The first bullet point ...
2 votes
1 answer
141 views

Mixing memory orders with/out conditionals

Consider the following: std::atomic_bool disabled = true; int counter1 = 0, counter2 = 1; [Thread 1] while (...) { counter2 = ...; if (disabled.load(std::memory_order::acquire) && ...
0 votes
1 answer
78 views

Are object parameters of synchronized block guranteed visibilty Synchronized Java

I cannot find an answer for the following case: public class Example { int a=0; public synchronized void method(Object x){ a++; x.value=x.value+1; } } I know that ...
0 votes
1 answer
46 views

Are these codes(concurrent C codes) necessary to use memory barrier?

I found these codes in project libuv 1.3.0. But I can't understand that why memory barriers(for compiler) are need. static int uv__async_make_pending(int* pending) { /* Do a cheap read first. */ ...
3 votes
1 answer
164 views

Memory order around atomic operations

I want to build a good mental image about std::memory_order_seq_cst, std::memory_order_relaxed, memory_order_acq_rel. Imagine your code as a sequence of bricks. Each brick is an operation on some ...
0 votes
1 answer
148 views

How to use memory barriers (instead of fetch_add) to make addition atomic and thread safe

The following is a simple example of two threads doing addition operations. #include <iostream> #include <atomic> #include <thread> std::atomic<int> atomic_int(0); int count =...
0 votes
1 answer
106 views

Java memory visibility outside the synchronized lock

Does the synchronized lock guarantee the following code always print 'END'? public class Visibility { private static int i = 0; public static void main(String[] args) throws ...
6 votes
3 answers
3k views

Is atomic_thread_fence(memory_order_release) different from using memory_order_acq_rel?

cppreference.com provides this note about std::atomic_thread_fence (emphasis mine): atomic_thread_fence imposes stronger synchronization constraints than an atomic store operation with the same std:...
0 votes
0 answers
40 views

Does the Intel SDM's "Intra-Processor Forwarding Is Allowed" memory ordering litmus test for store-forwarding show LoadLoad reordering?

This behavior can be represented by the following figure: But in my opinion, this memory older violates the order of load ->load. For Core 1, Load 1 executes after Load 2. In x86 memory ordering: ...
4 votes
2 answers
205 views

Where is the definition of the acquire operation in the C++ 20?

I am reading the std::memory_order on the cppreference.com and the C++ 20 specification. I can't find the definition of the terms, "acquire operation" and "release operation". The ...
2 votes
3 answers
774 views

PCIe ordering rules and x86, how are they compatible?

PCIe specs express clearly what are the ordering rules. A Posted Request must not pass another Posted Request A Posted Request must be able to pass Non-Posted Requests to avoid deadlocks It means ...
1 vote
1 answer
68 views

Successive compute kernels in vulkan

I have three compute kernels that have to run in succession, synchronized so that the previous kernel finishes before the next kernel starts. This is because a previous kernel writes a buffer that is ...
1 vote
3 answers
107 views

Reorder relaxed atomic operations on the same object

http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync Assuming x is initially 0: -Thread 1- x.store (1, memory_order_relaxed) x.store (2, memory_order_relaxed) -Thread 2- y = x.load (memory_order_relaxed) ...
1 vote
0 answers
59 views

Memory ordering and RMW operations

Suppose I make two relaxed modifications to two atomic objects in thread0, one per each object, and then make thread1 observe the modification that came second in thread0. Now without memory fences, ...
1 vote
0 answers
87 views

Confusing "Memory Barrier Example 1" in 《Memory Barriers: a Hardware View for Software Hackers》?

I am reading the great paper 《Memory Barriers: a Hardware View for Software Hackers》 written by Paul E. McKenney, which helps me a lot. But I came across a doubt in 《6.2 Example 1》: The author has ...

1
2 3 4 5
18