public void CompareAndSetInMultipleThreads()
 {
     AtomicLongArray a = new AtomicLongArray(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);
 }
Esempio n. 2
0
        public void CompareExistingValueAndSetNewValue()
        {
            AtomicLongArray ai = new AtomicLongArray(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]);
            }
        }
Esempio n. 3
0
        public void CompareAndSetInMultipleThreads()
        {
            AtomicLongArray a = new AtomicLongArray(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);
        }
Esempio n. 4
0
 public void Run()
 {
     for (;;)
     {
         bool done = true;
         for (int i = 0; i < ai.Count; ++i)
         {
             long v = ai[i];
             Assert.IsTrue(v >= 0);
             if (v != 0)
             {
                 done = false;
                 if (ai.CompareAndSet(i, v, v - 1))
                 {
                     Thread.VolatileWrite(ref counts, ++counts);
                 }
             }
         }
         if (done)
         {
             break;
         }
     }
 }
 public void CompareExistingValueAndSetNewValue()
 {
     AtomicLongArray ai = new AtomicLongArray(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]);
     }
 }