System.Threading.Interlocked.CompareChange使用

 1 class CompareChange
 2     {
 3         private static int i = 0;
 4         public static System.Threading.AutoResetEvent changeSignal = new System.Threading.AutoResetEvent(false);//同步信号
 5 
 6         public static void test()
 7         {
 8             ThreadSafe ts = new ThreadSafe();
 9 
10             System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
11             {
12                 Console.WriteLine("modifying-thread is waiting for signal");
13                 CompareChange.changeSignal.WaitOne();
14                 Console.WriteLine("modifying-thread received the signal");
15                 CompareChange.i++;
16                 ts.Total = CompareChange.i;
17                 Console.WriteLine("modifying-thread modify the CompareChange.i");
18                 CompareChange.changeSignal.Set();
19                 Console.WriteLine("modifying-thread send the complete signal");
20             });
21 
22             System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
23             {
24                 ts.AddToTotal(20);
25             });
26             
27             Console.ReadLine();
28         }
29     }
30 
31     public class ThreadSafe
32     {
33         // totalValue contains a running total that can be updated 
34         // by multiple threads. It must be protected from unsynchronized  
35         // access. 
36         private int totalValue = 0;
37         private int times = 0;
38         // The Total property returns the running total. 
39         public int Total
40         {
41             get { return totalValue; }
42             set { totalValue = value; }
43         }
44 
45         // AddToTotal safely adds a value to the running total. 
46         public int AddToTotal(int addend)
47         {
48             int initialValue, computedValue;
49             do
50             {
51                 this.times++;
52                 if (this.times == 2)
53                     Console.WriteLine("orignal value has changed.it's the "+this.times.ToString()+"th times to run the process");
54                 // Save the current running total in a local variable.
55                 initialValue = totalValue;
56 
57                 Console.WriteLine("sending the modifying signal");
58                 CompareChange.changeSignal.Set();
59 
60                 // Add the new value to the running total.
61                 computedValue = initialValue + addend;
62 
63                 Console.WriteLine("waiting for modifying-completely signal");
64                 CompareChange.changeSignal.WaitOne();
65 
66                 // CompareExchange compares totalValue to initialValue. If 
67                 // they are not equal, then another thread has updated the 
68                 // running total since this loop started. CompareExchange 
69                 // does not update totalValue. CompareExchange returns the 
70                 // contents of totalValue, which do not equal initialValue, 
71                 // so the loop executes again.
72             } while (initialValue != System.Threading.Interlocked.CompareExchange(
73                 ref totalValue, computedValue, initialValue));
74             // If no other thread updated the running total, then  
75             // totalValue and initialValue are equal when CompareExchange 
76             // compares them, and computedValue is stored in totalValue. 
77             // CompareExchange returns the value that was in totalValue 
78             // before the update, which is equal to initialValue, so the  
79             // loop ends. 
80             Console.WriteLine("process is over");
81             // The function returns computedValue, not totalValue, because 
82             // totalValue could be changed by another thread between 
83             // the time the loop ends and the function returns. 
84             return computedValue;
85         }
86     }

运行结果:

modifying-thread is waiting for signal
sending the modifying signal
waiting for modifying-completely signal
modifying-thread received the signal
modifying-thread modify the CompareChange.i
modifying-thread send the complete signal
orignal value has changed.it's the 2th times to run the process
sending the modifying signal
waiting for modifying-completely signal
process is over

 

 

posted @ 2014-08-19 11:29  LeonGo  阅读(467)  评论(0编辑  收藏  举报