예제 #1
0
        public void TestSharedMemoryThreaded()
        {
            using (ManualResetEvent mreStart = new ManualResetEvent(false))
                using (SharedMemoryStream shared = new SharedMemoryStream(5))
                    using (SharedMemoryStream copy = shared.Clone())
                    {
                        Thread[] allwriters = new Thread[Math.Max(1, Environment.ProcessorCount - 1)];
                        for (int tix = 0; tix < allwriters.Length; tix++)
                        {
                            allwriters[tix] = new Thread(
                                delegate()
                            {
                                using (SharedMemoryStream stream = shared.Clone())
                                {
                                    Random r = new Random();
                                    mreStart.WaitOne();
                                    for (int i = 0; i < 1000; i++)
                                    {
                                        PrimitiveSerializer.Int32.WriteTo(i, stream);
                                        Thread.SpinWait(r.Next(i));
                                    }
                                }
                            }
                                );
                            allwriters[tix].IsBackground = true;
                            allwriters[tix].Start();
                        }

                        mreStart.Set();
                        foreach (Thread t in allwriters)
                        {
                            Assert.IsTrue(t.Join(100));
                        }

                        for (int i = 0; i < 1000; i++)
                        {
                            int value = PrimitiveSerializer.Int32.ReadFrom(copy);
                            Assert.AreEqual(i, value);
                        }
                    }
        }
        public void TestSharedMemoryThreaded()
        {
            using (ManualResetEvent mreStart = new ManualResetEvent(false))
            using (SharedMemoryStream shared = new SharedMemoryStream(5))
            using (SharedMemoryStream copy = shared.Clone())
            {
                Thread[] allwriters = new Thread[Math.Max(1, Environment.ProcessorCount - 1)];
                for (int tix = 0; tix < allwriters.Length; tix++)
                {
                    allwriters[tix] = new Thread(
                        delegate()
                            {
                                using (SharedMemoryStream stream = shared.Clone())
                                {
                                    Random r = new Random();
                                    mreStart.WaitOne();
                                    for (int i = 0; i < 1000; i++)
                                    {
                                        PrimitiveSerializer.Int32.WriteTo(i, stream);
                                        Thread.SpinWait(r.Next(i));
                                    }
                                }
                            }
                        );
                    allwriters[tix].IsBackground = true;
                    allwriters[tix].Start();
                }

                mreStart.Set();
                foreach(Thread t in allwriters)
                    Assert.IsTrue(t.Join(100));

                for (int i = 0; i < 1000; i++)
                {
                    int value = PrimitiveSerializer.Int32.ReadFrom(copy);
                    Assert.AreEqual(i, value);
                }
            }
    	}