Questions tagged [memory-model]

For questions on memory ordering models at the programming language level (above the ISA or machine language level).

For questions on memory ordering models at the programming language level (above the ISA or machine language level). If the question is about a specific language, use also the tag for that language. Otherwise, if the question is not about any specific language or if the question is on the implementation of a memory ordering model in a language translation tool, use other tags such as language-agnostic, programming-languages, language-design, bytecode, compilation, or other related tags, as suitable. A very relevant tag is memory-fences, but the tags are not exactly synonyms.

Do not use this tag for questions on the memory ordering models that are strictly at the ISA or microarchitecture level unless pertinent (e.g, when implementing a memory ordering model for language in a compiler that targets a particular architecture). Instead, the memory-order tag should be used for such questions.

393 questions
30
votes
1 answer

C++11 memory_order_acquire and memory_order_release semantics?

http://en.cppreference.com/w/cpp/atomic/memory_order, and other C++11 online references, define memory_order_acquire and memory_order_release as: Acquire operation: no reads in the current thread can be reordered before this load. Release…
Cedomir Segulja
  • 357
  • 1
  • 3
  • 6
28
votes
5 answers

Could the JIT collapse two volatile reads as one in certain expressions?

Suppose we have a volatile int a. One thread does while (true) { a = 1; a = 0; } and another thread does while (true) { System.out.println(a+a); } Now, would it be illegal for a JIT compiler to emit assembly corresponding to 2*a…
aioobe
  • 383,660
  • 99
  • 774
  • 796
27
votes
3 answers

Is it possible to observe a partially-constructed object from another thread?

I've often heard that in the .NET 2.0 memory model, writes always use release fences. Is this true? Does this mean that even without explicit memory-barriers or locks, it is impossible to observe a partially-constructed object (considering…
Ani
  • 103,292
  • 21
  • 241
  • 294
27
votes
2 answers

Does empty synchronized(this){} have any meaning to memory visibility between threads?

I read this in an upvoted comment on StackOverflow: But if you want to be safe, you can add simple synchronized(this) {} at the end of you @PostConstruct [method] [note that variables were NOT volatile] I was thinking that happens-before is…
Piotr Müller
  • 4,845
  • 3
  • 45
  • 79
26
votes
3 answers

Does C have an equivalent of std::less from C++?

I was recently answering a question on the undefined behaviour of doing p < q in C when p and q are pointers into different objects/arrays. That got me thinking: C++ has the same (undefined) behaviour of < in this case, but also offers the standard…
26
votes
2 answers

Thread.VolatileRead Implementation

I'm looking at the implementation of the VolatileRead/VolatileWrite methods (using Reflector), and i'm puzzled by something. This is the implementation for VolatileRead: [MethodImpl(MethodImplOptions.NoInlining)] public static int VolatileRead(ref…
unknown
25
votes
2 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
  • 7,745
  • 5
  • 46
  • 107
25
votes
1 answer

python threading: memory model and visibility

Does python threading expose issues of memory visibility and statement reordering as Java does? Since I can't find any reference to a "Python Memory Model" or anything like that, despite the fact that lots of people are writing multithreaded Python…
philo
  • 3,249
  • 3
  • 21
  • 36
24
votes
3 answers

Acquire/release semantics with 4 threads

I am currently reading C++ Concurrency in Action by Anthony Williams. One of his listing shows this code, and he states that the assertion that z != 0 can fire. #include #include #include std::atomic
Aryan
  • 558
  • 4
  • 15
24
votes
2 answers

C++0x memory model and speculative loads/stores

So I was reading about the memory model that is part of the upcoming C++0x standard. However, I'm a bit confused about some of the restrictions for what the compiler is allowed to do, specifically about speculative loads and stores. To start with,…
janneb
  • 32,371
  • 2
  • 74
  • 90
24
votes
6 answers

Does Interlocked.CompareExchange use a memory barrier?

I'm reading Joe Duffy's post about Volatile reads and writes, and timeliness, and i'm trying to understand something about the last code sample in the post: while (Interlocked.CompareExchange(ref m_state, 1, 0) != 0) ; m_state = 0; while…
unknown
21
votes
1 answer

What are memory fences used for in Java?

Whilst trying to understand how SubmissionPublisher (source code in Java SE 10, OpenJDK | docs), a new class added to the Java SE in version 9, has been implemented, I stumbled across a few API calls to VarHandle I wasn't previously aware of:…
21
votes
3 answers

What are the C++11 memory ordering guarantees in this corner case?

I'm writing some lock-free code, and I came up with an interesting pattern, but I'm not sure if it will behave as expected under relaxed memory ordering. The simplest way to explain it is using an example: std::atomic a, b, c; auto a_local =…
Cameron
  • 86,330
  • 19
  • 177
  • 216
20
votes
2 answers

Can atomic loads be merged in the C++ memory model?

Consider the C++ 11 snippet below. For GCC and clang this compiles to two (sequentially consistent) loads of foo. (Editor's note: compilers do not optimize atomics, see this Q&A for more details, especially http://wg21.link/n4455 standards…
19
votes
5 answers

Peterson algorithm in Java?

Is there example implementation of Peterson algorithm for mutual exclusion in Java?
Gabriel Ščerbák
  • 16,864
  • 8
  • 33
  • 50
1
2
3
26 27