C # multithreading: Principle of volatile keywords

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.