memory-model

What is the C++ memory model for concurrency?

What is the C++ memory model for concurrency as defined by current standard? What about upcoming C++0x standard? Will it change the memory model to support concurrency better? ...

Does Delphi have any equivalent to C's volatile variable?

In C and C++ a variable can be marked as volatile, which means the compiler will not optimize it because it may be modified external to the declaring object. Is there an equivalent in Delphi programming? If not a keyword, maybe a work around? My thought was to use Absolute, but I wasn't sure, and that may introduce other side effects....

Differences between x86/x64/ia64 memory models on .NET

I'm looking for a reference on the differences between the memory models used by the .NET CLR/JIT on x86/x64/ia64. I know there's some differences between x86 and ia64 (instruction reordering, instruction removal, etc.), but I haven't found a reference on the differences between x86 and x64. I have an application that is aiming for s...

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 (Interlocked.CompareExchange(ref m_state, 1, 0) != 0) ; m_state = 0; … When the second CMPXCHG o...

Can a thread observe junk values in an object due to memory incoherency?

After a lot of research I believe I understand the JMM quite well, certainly well enough to know that when an object is shared between two threads you must synchronize all access on the same monitor. I understand that if multiple active threads access an object concurrently all bets are off as to what they will observe. However, if an ...

Memory barriers and large structs?

Let's say I've got a struct that consist of 100 bytes. What guarantees have I got about the following code? m_myLargeStruct = someValue; // copying 100 bytes Thread.MemoryBarrier(); // Executed by another thread, after "Thread.MemoryBarrier" was called by the first thread Console.WriteLine(m_myLargeStruct.ToString()); Does the memo...

Using memory barriers

In the following code sample, does the memory barrier in FuncA is required to ensure that the most up-to-date value is read? class Foo { DateTime m_bar; void FuncA() // invoked by thread X { Thread.MemoryBarrier(); // is required? Console.WriteLine(m_bar); } void FuncB() // invoked by thread Y { m_...

When do writes/reads affect main memory?

When I write a value into a field, what guarantees do I get regarding when the new value will be saved in the main memory? For example, how do I know that the processor don't keep the new value in it's private cache, but updated the main memory? Another example: int m_foo; void Read() // executed by thread X (on processor #0) { Co...

How does memory fences affect "freshness" of data?

I have a question about the following code sample (taken from: http://www.albahari.com/threading/part4.aspx#_NonBlockingSynch) class Foo { int _answer; bool _complete; void A() { _answer = 123; Thread.MemoryBarrier(); // Barrier 1 _complete = true; Thread.MemoryBarrier(); // Barrier 2 } ...

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 int address) { int num = address; MemoryBarrier(); return num; } How come t...

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, some of the relevant stuff: Hans Boehm's pages about threads and the memory model in C++0...

Is writing a reference atomic on 64bit VMs

Hi The java memory model mandates that writing a int is atomic: That is, if you write a value to it (consisting of 4 bytes) in one thread and read it in another, you will get all bytes or none, but never 2 new bytes and 2 old bytes or such. This is not guaranteed for long. Here, writing 0x1122334455667788 to a variable holding 0 before...

.NET multithreading, volatile and memory model

Assume that we have the following code: class Program { static volatile bool flag1; static volatile bool flag2; static volatile int val; static void Main(string[] args) { for (int i = 0; i < 10000 * 10000; i++) { if (i % 500000 == 0) { Console.WriteLine("{0:#,0}",i); } ...

Peterson algorithm in Java?

Is there example implementation of Peterson algorithm for mutual exclusion in Java? ...

C++0x: memory ordering

The current C++0x draft states on section 29.3.9 and 29.3.10, pages 1111-1112 that in the following example: // Thread 1 r1 = y.load(memory_order_relaxed); x.store(1, memory_order_relaxed); // Thread 2 r2 = x.load(memory_order_relaxed); y.store(1, memory_order_relaxed); The outcome r1 = r2 = 1 is possible since the operations of each...

Does the Java Memory Model (JSR-133) imply that entering a monitor flushes the CPU data cache(s)?

There is something that bugs me with the Java memory model (if i even understand everything correctly). If there are two threads A and B, there are no guarantees that B will ever see a value written by A, unless both A and B synchronize on the same monitor. For any system architecture that guarantees cache coherency between threads, the...

In C/C++, are volatile variables guaranteed to have eventually consistent semantics betwen threads?

Is there any guarantee by any commonly followed standard (ISO C or C++, or any of the POSIX/SUS specifications) that a variable (perhaps marked volatile), not guarded by a mutex, that is being accessed by multiple threads will become eventually consistent if it is assigned to? To provide a specific example, consider two threads sharing ...

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 code, I'm guessing that these gotchas don't exist here. No volatile keyword, for instance....