.NET 3.5SP1 64位内存模型与32位内存模型

时间:2010-06-07 16:53:33

标签: c# .net 64-bit 32-bit

据我了解,32位机器上的.NET内存模型保证32位字写入和读取是原子操作,但不能在64位字上提供此保证。我已经编写了一个快速工具来在Windows XP 32位操作系统上演示这种效果,并且结果与内存模型描述一致。

但是,我已经使用了同一个工具的可执行文件并在Windows 7企业版64位操作系统上运行它,结果却截然不同。两台机器都是相同的规格,只是安装了不同的操作系统。我本以为.NET内存模型可以保证在64位操作系统上对32位和64位字的写入和读取都是原子的。我发现结果与BOTH假设完全相反。在此操作系统上,32位读取和写入未被证明是原子的。

有人可以向我解释为什么在64位操作系统上失败了吗?

工具代码:

using System;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var th = new Thread(new ThreadStart(RunThread));
            var th2 = new Thread(new ThreadStart(RunThread));
            int lastRecordedInt = 0;
            long lastRecordedLong = 0L;
            th.Start();
            th2.Start();
            while (!done)
            {
                int newIntValue = intValue;
                long newLongValue = longValue;
                if (lastRecordedInt > newIntValue) Console.WriteLine("BING(int)! {0} > {1}, {2}", lastRecordedInt, newIntValue, (lastRecordedInt - newIntValue));
                if (lastRecordedLong > newLongValue) Console.WriteLine("BING(long)! {0} > {1}, {2}", lastRecordedLong, newLongValue, (lastRecordedLong - newLongValue));
                lastRecordedInt = newIntValue;
                lastRecordedLong = newLongValue;
            }
            th.Join();
            th2.Join();
            Console.WriteLine("{0} =? {2}, {1} =? {3}", intValue, longValue, Int32.MaxValue / 2, (long)Int32.MaxValue + (Int32.MaxValue / 2));
        }

        private static long longValue = Int32.MaxValue;
        private static int intValue;
        private static bool done = false;

        static void RunThread()
        {
            for (int i = 0; i < Int32.MaxValue / 4; ++i)
            {
                ++longValue;
                ++intValue;
            }
            done = true;
        }
    }
}

Windows XP 32位上的结果:

Windows XP 32-bit
Intel Core2 Duo P8700 @ 2.53GHz
BING(long)! 2161093208 > 2161092246, 962
BING(long)! 2162448397 > 2161273312, 1175085
BING(long)! 2270110050 > 2270109040, 1010
BING(long)! 2270115061 > 2270110059, 5002
BING(long)! 2558052223 > 2557528157, 524066
BING(long)! 2571660540 > 2571659563, 977
BING(long)! 2646433569 > 2646432557, 1012
BING(long)! 2660841714 > 2660840732, 982
BING(long)! 2661795522 > 2660841715, 953807
BING(long)! 2712855281 > 2712854239, 1042
BING(long)! 2737627472 > 2735210929, 2416543
1025780885 =? 1073741823, 3168207035 =? 3221225470

注意如何从未编写BING(int)并证明32位读取/写入在此32位操作系统上是原子的。

Windows 7企业版64位上的结果:

Windows 7 Enterprise 64-bit
Intel Core2 Duo P8700 @ 2.53GHz
BING(long)! 2208482159 > 2208121217, 360942
BING(int)! 280292777 > 279704627, 588150
BING(int)! 308158865 > 308131694, 27171
BING(long)! 2549116628 > 2548884894, 231734
BING(int)! 534815527 > 534708027, 107500
BING(int)! 545113548 > 544270063, 843485
BING(long)! 2710030799 > 2709941968, 88831
BING(int)! 668662394 > 667539649, 1122745
1006355562 =? 1073741823, 3154727581 =? 3221225470

请注意,BING(long)AND BING(int)都会显示!为什么32位操作失败,更不用说64位操作了?

1 个答案:

答案 0 :(得分:4)

在你的线程回调中,你所做的不仅仅是写作或阅读:

++longValue;
++intValue;

无法保证读取和写入都是原子的。使用Interlocked.Increment确保此操作的原子性。