Exemple #1
0
        public void AccessResource()
        {
            spinLock.Enter();

            // 一次一个线程访问资源

            spinLock.Leave();
        }
    public static void Go()
    {
        Int32       x          = 0;
        const Int32 iterations = 10000000; // 10 million

        // How long does it take to increment x 10 million times?
        Stopwatch sw = Stopwatch.StartNew();

        for (Int32 i = 0; i < iterations; i++)
        {
            x++;
        }
        Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling a method that does nothing?
        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            M(); x++; M();
        }
        Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleSpinLock?
        SimpleSpinLock ssl = new SimpleSpinLock();

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            ssl.Enter(); x++; ssl.Leave();
        }
        Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SpinLock?
        SpinLock sl = new SpinLock(false);

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            Boolean taken = false;
            sl.Enter(ref taken); x++; sl.Exit(false);
        }
        Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleWaitLock?
        using (SimpleWaitLock swl = new SimpleWaitLock()) {
            sw.Restart();
            for (Int32 i = 0; i < iterations; i++)
            {
                swl.Enter(); x++; swl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
        }
        Console.ReadLine();
    }
 public void AccessResource()
 {
     try
     {
         _spinLock.Enter();
         // Note: Здесь доступ к ресурсу в любой момент времени имеет только один поток
     }
     finally
     {
         _spinLock.Leave();
     }
 }
Exemple #4
0
        public static void Go()
        {
            int       x          = 0;
            const int iterations = 1000000;
            Stopwatch sw         = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                x++;
            }
            Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                M(); x++; M();
            }
            Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

            SimpleSpinLock ssl = new SimpleSpinLock();

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                ssl.Enter(); x++; ssl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleSpinLock:{0:N0}", sw.ElapsedMilliseconds);

            SpinLock sl = new SpinLock(false);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                bool taken = false;
                sl.Enter(ref taken); x++; sl.Exit(false);
            }
            Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

            using (SimpleWaitLock swl = new SimpleWaitLock())
            {
                sw.Restart();
                for (int i = 0; i < iterations; i++)
                {
                    swl.Enter(); x++; swl.Leave();
                }
                Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
            }
            Console.ReadLine();
        }
        public void SimpleSpinLock_Benchmark()
        {
            var m_sl  = new SimpleSpinLock();
            var model = new DummyModelLock();

            void Action()
            {
                m_sl.Enter();
                for (int i = 0; i < Size; i++)
                {
                    try
                    {
                        model.Counter += (i % 10);
                    }
                    finally
                    {
                    }
                }
                m_sl.Leave();
            }

            Parallel.Invoke(Action, Action, Action);
        }
   public static void Go() {
      Int32 x = 0;
      const Int32 iterations = 10000000;  // 10 million

      // How long does it take to increment x 10 million times?
      Stopwatch sw = Stopwatch.StartNew();
      for (Int32 i = 0; i < iterations; i++) {
         x++;
      }
      Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling a method that does nothing?
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         M(); x++; M();
      }
      Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleSpinLock?
      SimpleSpinLock ssl = new SimpleSpinLock();
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         ssl.Enter(); x++; ssl.Leave();
      }
      Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SpinLock?
      SpinLock sl = new SpinLock(false);
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         Boolean taken = false;
         sl.Enter(ref taken); x++; sl.Exit(false);
      }
      Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleWaitLock?
      using (SimpleWaitLock swl = new SimpleWaitLock()) {
         sw.Restart();
         for (Int32 i = 0; i < iterations; i++) {
            swl.Enter(); x++; swl.Leave();
         }
         Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
      }
      Console.ReadLine();
   }
Exemple #7
0
 public void AccessBySimpleSpinLock()
 {
     sslock.Enter();
     _resource++;
     sslock.Leave();
 }
Exemple #8
0
 public void AccessResource()
 {
     m_sl.Enter();
     m_sl.Leave();
 }
Exemple #9
0
 public void AddPage(Page page)
 {
     _lock.Enter();
     _pageDic.Add(page.index, page);
     _lock.Leave();
 }