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.

This tag is for questions that are about the consequences of memory barriers, or whether or not a memory barrier is needed.

742 questions
73
votes
4 answers

Is function call an effective memory barrier for modern platforms?

In a codebase I reviewed, I found the following idiom. void notify(struct actor_t act) { write(act.pipe, "M", 1); } // thread A sending data to thread B void send(byte *data) { global.data = data; notify(threadB); } // in thread B event…
mikebloch
  • 1,577
  • 11
  • 21
70
votes
2 answers

How do I Understand Read Memory Barriers and Volatile

Some languages provide a volatile modifier that is described as performing a "read memory barrier" prior to reading the memory that backs a variable. A read memory barrier is commonly described as a way to ensure that the CPU has performed the reads…
Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
58
votes
2 answers

Does it make any sense to use the LFENCE instruction on x86/x86_64 processors?

Often in internet I find that LFENCE makes no sense in processors x86, ie it does nothing , so instead MFENCE we can absolutely painless to use SFENCE, because MFENCE = SFENCE + LFENCE = SFENCE + NOP = SFENCE. But if LFENCE does not make sense, then…
Alex
  • 12,578
  • 15
  • 99
  • 195
55
votes
6 answers

Why we need Thread.MemoryBarrier()?

In "C# 4 in a Nutshell", the author shows that this class can write 0 sometimes without MemoryBarrier, though I can't reproduce in my Core2Duo: public class Foo { int _answer; bool _complete; public void A() { _answer = 123; …
Felipe Pessoto
  • 6,855
  • 10
  • 42
  • 73
52
votes
7 answers

Are mutex lock functions sufficient without volatile?

A coworker and I write software for a variety of platforms running on x86, x64, Itanium, PowerPC, and other 10 year old server CPUs. We just had a discussion about whether mutex functions such as pthread_mutex_lock() ... pthread_mutex_unlock() are…
David
  • 1,023
  • 1
  • 8
  • 16
40
votes
2 answers

Why do I need a memory barrier?

C# 4 in a Nutshell (highly recommended btw) uses the following code to demonstrate the concept of MemoryBarrier (assuming A and B were run on different threads): class Foo{ int _answer; bool complete; void A(){ _answer = 123; …
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
36
votes
2 answers

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 output signal is asserted, requests from other…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
36
votes
2 answers

Behavior of memory barrier in Java

After reading more blogs/articles etc, I am now really confused about the behavior of load/store before/after memory barrier. Following are 2 quotes from Doug Lea in one of his clarification article about JMM, which are both very…
asticx
  • 643
  • 2
  • 8
  • 14
36
votes
2 answers

When is a compiler-only memory barrier (such as std::atomic_signal_fence) useful?

The notion of a compiler fence often comes up when I'm reading about memory models, barriers, ordering, atomics, etc., but normally it's in the context of also being paired with a CPU fence, as one would expect. Occasionally, however, I read about…
etherice
  • 1,761
  • 15
  • 25
32
votes
3 answers

Can atomics suffer spurious stores?

In C++, can atomics suffer spurious stores? For example, suppose that m and n are atomics and that m = 5 initially. In thread 1, m += 2; In thread 2, n = m; Result: the final value of n should be either 5 or 7, right? But could it…
thb
  • 13,796
  • 3
  • 40
  • 68
32
votes
3 answers

Does std::mutex create a fence?

If I lock a std::mutex will I always get a memory fence? I am unsure if it implies or enforces you to get the fence. Update: Found this reference following up on RMF's comments. Multithreaded programming and memory visibility
Tom Kerr
  • 10,444
  • 2
  • 30
  • 46
31
votes
3 answers

Memory fences: acquire/load and release/store

My understanding of std::memory_order_acquire and std::memory_order_release is as follows: Acquire means that no memory accesses which appear after the acquire fence can be reordered to before the fence. Release means that no memory accesses which…
Siler
  • 8,976
  • 11
  • 64
  • 124
30
votes
4 answers

Does a memory barrier ensure that the cache coherence has been completed?

Say I have two threads that manipulate the global variable x. Each thread (or each core I suppose) will have a cached copy of x. Now say that Thread A executes the following instructions: set x to 5 some other instruction Now when set x to 5 is…
30
votes
4 answers

Should thread-safe class have a memory barrier at the end of its constructor?

When implementing a class intended to be thread-safe, should I include a memory barrier at the end of its constructor, in order to ensure that any internal structures have completed being initialized before they can be accessed? Or is it the…
Douglas
  • 53,759
  • 13
  • 140
  • 188
29
votes
2 answers

C++ Memory Barriers for Atomics

I'm a newbie when it comes to this. Could anyone provide a simplified explanation of the differences between the following memory barriers? The windows MemoryBarrier(); The fence _mm_mfence(); The inline assembly asm volatile ("" : : :…
AJG85
  • 15,849
  • 13
  • 42
  • 50
1
2 3
49 50