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.

549 questions
70
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,527
  • 11
  • 21
61
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
  • 15,147
  • 9
  • 54
  • 79
48
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 Fujiy Pessoto
  • 6,492
  • 8
  • 40
  • 71
47
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
  • 11,148
  • 13
  • 83
  • 169
46
votes
5 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
  • 963
  • 6
  • 15
37
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,151
  • 9
  • 40
  • 60
34
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
  • 613
  • 1
  • 7
  • 14
34
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,681
  • 13
  • 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
  • 11,861
  • 3
  • 35
  • 63
31
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
  • 9,632
  • 1
  • 26
  • 43
28
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
  • 14,891
  • 12
  • 40
  • 50
28
votes
2 answers

Atomicity 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…
Gilgamesz
  • 3,855
  • 2
  • 19
  • 47
27
votes
1 answer

Memory barriers and the TLB

Memory barriers guarantee that the data cache will be consistent. However, does it guarantee that the TLB will be consistent? I am seeing a problem where the JVM (java 7 update 1) sometimes crashes with memory errors (SIGBUS, SIGSEG) when passing a…
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
27
votes
2 answers

Memory model ordering and visibility?

I tried looking for details on this, I even read the standard on mutexes and atomics... but still I couldnt understand the C++11 memory model visibility guarantees. From what I understand the very important feature of mutex BESIDE mutual exclusion…
NoSenseEtAl
  • 23,776
  • 22
  • 102
  • 222
27
votes
3 answers

Memory barrier generators

Reading Joseph Albahari's threading tutorial, the following are mentioned as generators of memory barriers: C#'s lock statement (Monitor.Enter/Monitor.Exit) All methods on the Interlocked class Asynchronous callbacks that use the thread pool —…
Ohad Schneider
  • 33,142
  • 10
  • 150
  • 190
1
2 3
36 37