Ejemplo n.º 1
0
        public void CompareExistingValueAndSetNewValue()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.IsTrue(ai.CompareAndSet(i, 1, 2));
                Assert.IsTrue(ai.CompareAndSet(i, 2, -4));
                Assert.AreEqual(-4, ai[i]);
                Assert.IsFalse(ai.CompareAndSet(i, -5, 7));
                Assert.IsFalse(7 == ai[i]);
                Assert.IsTrue(ai.CompareAndSet(i, -4, 7));
                Assert.AreEqual(7, ai[i]);
            }
        }
Ejemplo n.º 2
0
        public void CompareAndSetInMultipleThreads()
        {
            AtomicIntegerArray a = new AtomicIntegerArray(1);

            a[0] = 1;
            Thread t = ThreadManager.StartAndAssertRegistered(
                "T1", () => { while (!a.CompareAndSet(0, 2, 3))
                              {
                                  Thread.Sleep(0);
                              }
                });

            Assert.IsTrue(a.CompareAndSet(0, 1, 2));
            ThreadManager.JoinAndVerify(Delays.Long);
            Assert.IsFalse(t.IsAlive);
            Assert.AreEqual(a[0], 3);
        }
Ejemplo n.º 3
0
 public void Run()
 {
     for (; ;)
     {
         bool done = true;
         for (int i = 0; i < ai.Count; ++i)
         {
             int v = ai[i];
             Assert.IsTrue(v >= 0);
             if (v != 0)
             {
                 done = false;
                 if (ai.CompareAndSet(i, v, v - 1))
                 {
                     ++counts;
                 }
             }
         }
         if (done)
         {
             break;
         }
     }
 }