今天突然理解了“volatile”

时间:2021-03-28 15:49:22

首先咱们可以看一下MSDN对volatile的定义:
The volatile keyword indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment.

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.

然后呢,咱们可以看到在Singleton模式中对多线程的优化

 1 今天突然理解了“volatile”using  System;
 2 今天突然理解了“volatile” using  System.Configuration;
 3 今天突然理解了“volatile”
 4 今天突然理解了“volatile” namespace  HeadFirstDesignPatterns.Singleton.InterestRate
 5 今天突然理解了“volatile”今天突然理解了“volatile” {
 6今天突然理解了“volatile”今天突然理解了“volatile”    /// <summary>
 7今天突然理解了“volatile”    /// Summary description for RateSingleton.
 8今天突然理解了“volatile”    /// </summary>

 9今天突然理解了“volatile”    public class RateSingleton
10今天突然理解了“volatile”今天突然理解了“volatile”    {
11今天突然理解了“volatile”        private volatile static RateSingleton uniqueInstance;
12今天突然理解了“volatile”        private static object syncRoot = new Object();
13今天突然理解了“volatile”
14今天突然理解了“volatile”        private double currentRate = 
15今天突然理解了“volatile”            Convert.ToDouble(ConfigurationSettings.AppSettings["CurrentInterestRate"]);
16今天突然理解了“volatile”
17今天突然理解了“volatile”        public double CurrentRate
18今天突然理解了“volatile”今天突然理解了“volatile”        {
19今天突然理解了“volatile”            get
20今天突然理解了“volatile”今天突然理解了“volatile”            {
21今天突然理解了“volatile”                return currentRate;
22今天突然理解了“volatile”            }

23今天突然理解了“volatile”            set
24今天突然理解了“volatile”今天突然理解了“volatile”            {
25今天突然理解了“volatile”                currentRate = value;
26今天突然理解了“volatile”            }

27今天突然理解了“volatile”        }

28今天突然理解了“volatile”
29今天突然理解了“volatile”        private RateSingleton()
30今天突然理解了“volatile”今天突然理解了“volatile”        {}
31今天突然理解了“volatile”
32今天突然理解了“volatile”        public static RateSingleton GetInstance()
33今天突然理解了“volatile”今天突然理解了“volatile”        {
34今天突然理解了“volatile”            //The approach below ensures that only one instance is created and only 
35今天突然理解了“volatile”            //when the instance is needed. Also, the uniqueInstance variable is 
36今天突然理解了“volatile”            //declared to be volatile to ensure that assignment to the instance variable 
37今天突然理解了“volatile”            //completes before the instance variable can be accessed. Lastly, 
38今天突然理解了“volatile”            //this approach uses a syncRoot instance to lock on, rather than 
39今天突然理解了“volatile”            //locking on the type itself, to avoid deadlocks.
40今天突然理解了“volatile”
41今天突然理解了“volatile”            if(uniqueInstance == null)
42今天突然理解了“volatile”今天突然理解了“volatile”            {
43今天突然理解了“volatile”                lock (syncRoot)
44今天突然理解了“volatile”今天突然理解了“volatile”                {
45今天突然理解了“volatile”                    if(uniqueInstance == null)
46今天突然理解了“volatile”今天突然理解了“volatile”                    {
47今天突然理解了“volatile”                        uniqueInstance = new RateSingleton();
48今天突然理解了“volatile”                    }

49今天突然理解了“volatile”                }

50今天突然理解了“volatile”            }

51今天突然理解了“volatile”            return uniqueInstance;
52今天突然理解了“volatile”        }

53今天突然理解了“volatile”    }

54今天突然理解了“volatile”}

55 今天突然理解了“volatile”