线程原子性操作问题
代码:
class TestNoInterLocked {
private long i;
private Dictionary<string, bool> diction = new Dictionary<string,bool>();
private EventWaitHandle e = new EventWaitHandle(false, EventResetMode.ManualReset);
public void Test() {
for (int j = 0; j < 10; j++) {
Thread t = new Thread(new ThreadStart(Test1));
t.Name = "Thread "+j;
diction.Add(t.Name, false);
t.Start();
}
e.WaitOne();
Console.WriteLine("i====================={0}",i);
}
public void Test1() {
int k =0;
while (k++ < 150) {
i++; //这个地方不是原子性操作,也就是某个线程可能正要赋值时,被其它线程打断,
//也就是两个线程可能只对i的值加了一次,因此我觉得i应该是小于等于1500的
//那么我为什么每次执行打出i的值都是1500呢,请指教哈
Console.WriteLine(Thread.CurrentThread.Name+"="+i);
}
diction[Thread.CurrentThread.Name] = true;
bool b = true;
foreach (string key in diction.Keys) {
b = b & diction[key];
}
if (b)
e.Set();
}
[MTAThread]
public static void Main()
{
TestNoInterLocked tNo = new TestNoInterLocked();
tNo.Test();
Console.ReadLine();
}
}
[解决办法]
你机器的cpu数还不至于导致出现这种情况。一个cpu基本上是不会出现的。
另外每个线程只循环150次太少,启动线程也是需要时间的,可能第二个线程还没起来,第一个线程的150次已经做完了。
[解决办法]