Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile fields in C#

From the specification 10.5.3 Volatile fields:


The type of a volatile field must be one of the following:

  • A reference-type.

  • The type byte, sbyte, short, ushort, int, uint, char, float, bool, System.IntPtr, or System.UIntPtr.

  • An enum-type having an enum base type of byte, sbyte, short, ushort, int, or uint.


First I want to confirm my understanding is correct: I guess the above types can be volatile because they are stored as a 4-bytes unit in memory(for reference types because of its address), which guarantees the read/write operation is atomic. A double/long/etc type can't be volatile because they are not atomic reading/writing since they are more than 4 bytes in memory. Is my understanding correct?

And the second, if the first guess is correct, why a user defined struct with only one int field in it(or something similar, 4 bytes is ok) can't be volatile? Theoretically it's atomic right? Or it's not allowed simply because that all user defined structs(which is possibly more than 4 bytes) are not allowed to volatile by design?

like image 893
Cheng Chen Avatar asked Feb 25 '11 03:02

Cheng Chen


People also ask

What are volatile variables in C?

C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.

What is a volatile field?

Volatile keyword is a field modifier. Synchronized keyword modifies code blocks and methods. The thread cannot be blocked for waiting in case of volatile. Threads can be blocked for waiting in case of synchronized.

What is volatile structure in C?

C's volatile keyword is a qualifier that can be used to declare a variable in such a way that the compiler will never optimize away any of the reads and writes.

What is volatile datatype?

volatile is a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treat the variable. Declaring a variable volatile is a directive to the compiler.


1 Answers

So, I suppose you propose the following point to be added:

  • A value type consisting only of one field which can be legally marked volatile.

First, fields are usually private, so in external code, nothing should depend on a presence of a certain field. Even though the compiler has no issue accessing private fields, it is not such a good idea to restrict a certain feature based on something the programmer has no proper means to affect or inspect.

Since a field is usually a part of the internal implementation of a type, it can be changed at any time in a referenced assembly, but this could make a piece of C# code that used the type illegal.

This theoretical and practical reason means that the only feasible way would be to introduce a volatile modifier for value types that would ensure that point specified above holds. However, since the only group of types that would benefit from this modifier are value types with a single field, this feature probably wasn't very high on the list.

like image 60
IS4 Avatar answered Sep 25 '22 07:09

IS4