I was trying out a code sample from this book that should demonstrate that the post decrement operator is not atomic. The code is as I have entered it into LinqPad.
void Main() {
var count = 0;
do {
_x = 10000;
for (int i = 0; i < 100; i++) {
new Thread(Go).Start();
}
Thread.Sleep(1000);
Console.WriteLine("Try "+ count);
count++;
} while (_x == 0);
Console.WriteLine(_x);
}
int _x = 10000;
void Go() { for (int i = 0; i < 100; i++) _x--; }
想法是,在不设锁的多个环线上同时进行增减<条码>_x,否则,当所有胎面均已完结时,可产生<条码>-x条码>值。
My problem is that no matter how long I seem to try I always get 0 as a result. I have run the code on two different computers (both Windows 7) and two different versions of .NET and both give me the same result.
What am I missing here?