The volatile keyword in C # (I only know today)

Source: Internet
Author: User

In fact, I still know this keyword when I look at Terrylee's Singleton mode!

Let's take a look at msdn's explanation: http://msdn.microsoft.com/zh-cn/library/x13ttww7.aspx

The volatile keyword indicates that a field can be modified by multiple concurrent threads. Fields declared as volatile are not restricted by Compiler Optimization (assuming that they are accessed by a single thread. This ensures that the field displays the latest value at any time.

The volatile modifier is usually used for fields accessed by multiple threads but not serialized by using the lock statement. For examples of using volatile in A multithreaded solution, see How to: create and terminate a thread (C # programming guide ).

The volatile keyword can be applied to the following fields:

  • Reference type.

  • Pointer type (in an insecure context ). Note that although the pointer itself can be variable, the object it points to cannot be variable. In other words, you cannot declare a pointer to a mutable object ".

  • 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.

Variable keywords can only be applied to class or structure fields. Local variables cannot be declared as volatile.

Paste Terrylee's Code directly: (tailism)

Using System; namespace SigletonPattern. Sigleton {// <summary> // What is the result? Yes? :? In? C # use? Double? ? Lock? OK? Real? Now? Single? ? Module? ? /// Edit? Write? :? Terrylee // day? Period? :? Year? Month? ? /// </Summary> public class DoubLockSigleton {private static volatile DoubLockSigleton instance; /// <summary> // auxiliary? Help? Lock? Right? Like ?,? Ben? Body? No? Yes? Meaning? ? /// </Summary> private static object syncRoot = new Object (); // <summary> /// what is the structure? Creation? Fang? Method? Change? Is it? Private /// </summary> private DoubLockSigleton () {} public static DoubLockSigleton Instance {get {if (instance = null) {lock (syncRoot) {if (instance = null) instance = new DoubLockSigleton () ;}} return instance ;}
        }    }}

 

Volatile I still don't quite understand his purpose! First understand this keyword! Hey

Technorati labels: volatile, asp.net

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.