首页 > 代码库 > System.Threading.Interlocked.CompareChange使用
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 ThreadSafe32 {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 = http://www.mamicode.com/0;37 private int times = 0;38 // The Total property returns the running total. 39 public int Total40 {41 get { return totalValue; }42 set { totalValue =http://www.mamicode.com/ 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 do50 {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 =http://www.mamicode.com/ 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 = http://www.mamicode.com/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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。