using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace Buff { // [StructLayout(LayoutKind.Explicit, Size = 120)] // class Tclass // { // [FieldOffset(56)] // public long x = 0l; // } class Tclass { public long x = 0l; } internal class T01_CacheLinePadding { public static Tclass[] arr = new Tclass[2]; public static long COUNT = 10_0000_0000l; public static System.DateTime startTime; public static void Main(string[] args) { arr[0] = new Tclass(); arr[1] = new Tclass(); startTime = System.DateTime.Now; //Run(); RunOne(); } public static void RunOne() { for (int j = 0; j < COUNT; j++) { arr[0].x = j; arr[1].x = j; } DateTime dt2 = System.DateTime.Now; TimeSpan ts = dt2.Subtract(startTime); Console.WriteLine(string.Format("当前t1的时间:{0}",ts.TotalMilliseconds)); } public static void Run() { List<ManualResetEvent> list = new List<ManualResetEvent>(); ManualResetEvent mre = new ManualResetEvent(false); System.Threading.Thread thread = new Thread((o) => { for (int j = 0; j < COUNT; j++) { arr[0].x = j; } ((ManualResetEvent)o).Set(); }); System.Threading.Thread thread1 = new Thread((o) => { for (int j = 0; j < COUNT; j++) { arr[1].x = j; } ((ManualResetEvent)o).Set(); }); list.Add(mre); thread.Start(mre); thread1.Start(mre); WaitHandle.WaitAll(list.ToArray()); DateTime dt2 = System.DateTime.Now; TimeSpan ts = dt2.Subtract(startTime); Console.WriteLine(string.Format("当前t1的时间:{0}",ts.TotalMilliseconds)); } } }
12-26
1064
06-16
1231
07-09
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交