C # multithreading: Principle of volatile keywords from: blog garden font size: [large, small]
- Abstract:The volatile keyword is only used for class or structure fields. It is used to notify the compiler that multiple threads will access this field. Therefore, it should not optimize the status of this Member, this ensures that the field displays the latest value at any time.
Volatile keywords
The volatile keyword is only used for class or structure fields. It is used to notify the compiler that multiple threads will access this field. Therefore, it should not optimize the status of this Member, this ensures that the field displays the latest value at any time.
Not all types can be defined as volatile fields. Only the following types can be defined as volatile:
Reference type.
Pointer type (in an insecure context ).
Integer, such as sbyte, byte, short, ushort, Int, uint, Char, float, and bool.
An Enumeration type with an integer base type.
It is known that it is a generic parameter of the reference type.
Intptr and uintptr.
Note that only the type of value or the number of referenced digits that does not exceed the number of digits of the native integer value (4 bytes in 32-bit System) can become volatile. Why? My understanding is: the compiler ensures that the volatile field always obtains the latest value in the case of multiple threads. The most important thing is the atomicity of the volatile field operation, that is, the locally compiledCodeYou can assign values to the volatile field using only one machine command. How can we ensure the atomicity of operations? In a 32-bit system, any data operation is based on 4 bytes. A value assignment operation smaller than 4 bytes can be executed by a specified machine. However, if the field occupies more than 4 bytes of memory, the machine command that generates the value assignment statement must be greater than one. In this way, in multi-thread context switching, the value may have just been assigned to half of the value, it is switched to another thread. In this way, the volatile field cannot always present a complete and valid latest value in a multi-threaded environment.
The above is my understanding of the volatile keyword. If you have any opinions, please post your own comments.
Volatile keywords
The volatile keyword is only used for class or structure fields. It is used to notify the compiler that multiple threads will access this field. Therefore, it should not optimize the status of this Member, this ensures that the field displays the latest value at any time.
Not all types can be defined as volatile fields. Only the following types can be defined as volatile:
Reference type.
Pointer type (in an insecure context ).
Integer, such as sbyte, byte, short, ushort, Int, uint, Char, float, and bool.
An Enumeration type with an integer base type.
It is known that it is a generic parameter of the reference type.
Intptr and uintptr.
Note that only the type of value or the number of referenced digits that does not exceed the number of digits of the native integer value (4 bytes in 32-bit System) can become volatile. Why? My understanding is: the compiler ensures that the volatile field always obtains the latest value in the case of multiple threads. The most important thing is the atomicity of volatile field operations, that is, the locally compiled code can assign values to the volatile field only using one machine command. How can we ensure the atomicity of operations? In a 32-bit system, any data operation is based on 4 bytes. A value assignment operation smaller than 4 bytes can be executed by a specified machine. However, if the field occupies more than 4 bytes of memory, the machine command that generates the value assignment statement must be greater than one. In this way, in multi-thread context switching, the value may have just been assigned to half of the value, it is switched to another thread. In this way, the volatile field cannot always present a complete and valid latest value in a multi-threaded environment.
The above is my understanding of the volatile keyword. If you have any opinions, please post your own comments.