. Net memory model, variables and test sets: what is guarantee?

I Know. The. Net memory model (in the. Net framework; not compact / micro / Silverlight / mono / XNA / what have you you you you you you you) ensures that certain types of operations (most notably basic integers and references) are guaranteed to be atomic

In addition, I believe the x86 / x64 test and setup instructions (and interlocked. Compareexchange) actually refer to the global memory location, so if it succeeds, another interlocked Compareexchange will see the new value

Finally, I believe that the volatile keyword is that the compiler propagates read & writes to ASAP and does not reorder operations on this variable (pair?)

This leads to several problems:

Is my belief correct? > Interlocked. Read has no overload of int, only long (this is two words, so it is usually not read by atoms) I always assume Net memory model ensures that you can see the latest value when reading int / reference, but uses processor cache, registers, etc I began to see that this might not be possible Is there any way to force variables to be re referenced? > Is there enough volatility to solve the above problems of integers and references? > In x86 / x64, I can assume

If there are two global integer variables X and y, both of which are initialized to 0, if I write:

x = 1;
y = 2;

The no thread will see x = 0 and y = 2 (that is, writes will occur sequentially) If they are volatile, will this change?

Solution

>Only read and write variables up to 32 bits wide (64 bits wide on x64 systems) are atoms All this means that you won't read an int and get a half write value This does not mean that arithmetic is atomic

Summary:

>The compiler is free to reorder instructions. > The CPU is free to reorder instructions. > Word size reading and writing are atomic Arithmetic and other operations are not atomic because they involve reading, calculating, and then writing. > Reading the font size from memory will always retrieve the latest value But most of the time you don't know if you really read from memory. > Total memory impairment stops (1) and (2) Most compilers allow you to stop yourself (1). > Volatile keywordensure that you are reading from memory – (4). > Interlocking operations (lock prefixes) allow multiple operations to be atomic For example, read and write (interlocked exchange) Or read compare write (interlockedcompareexchange) They also act as memory impairment, so (1) and (2) are stopped They always write letters (obviously), so (4) are guaranteed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>