Exemplo n.º 1
0
        public void Test()
        {
            AtomicInteger m = new AtomicInteger(0);

            Assert.IsTrue(m == 0);

            Assert.IsFalse(m.CompareAndSet(3, 2));
            Assert.IsTrue(m == 0);

            Assert.IsTrue(m.CompareAndSet(0, 2));
            Assert.IsTrue(m == 2);

            var rs = m + 3;// 2+3

            Assert.AreEqual(5, rs);

            Assert.AreEqual(4, m--);

            Assert.AreEqual(2, m - 2);

            Assert.AreEqual(6, m + 4);

            Assert.AreEqual(6, m.GetAndAdd(4));
            Assert.AreEqual(10, m);
        }
Exemplo n.º 2
0
        public override IMember Next()
        {
            if (Members == null || Members.Length == 0)
            {
                return(null);
            }
            var length = Members.Length;

            return(Members[(_index.GetAndAdd(1) % length + length) % length]);
        }
Exemplo n.º 3
0
        public override void Update(int completed)
        {
            int old = pendingUpdates.GetAndAdd(completed);

            if (IsMainThread())
            {
                DoUpdates();
            }
            else
            {
                if (old == 0)
                {
                    process.Release();
                }
            }
        }
 protected override void OnReceive(object message)
 {
     if (message is string)
     {
         var s = (string)message;
         if (s == "end")
         {
             _latch.CountDown();
         }
     }
     if (message is int)
     {
         var i = (int)message;
         _counter.GetAndAdd(i);
     }
 }
Exemplo n.º 5
0
        public void Test()
        {
            AtomicInteger m = new AtomicInteger(0);
            Assert.IsTrue(m == 0);

            Assert.IsFalse(m.CompareAndSet(3, 2));
            Assert.IsTrue(m == 0);

            Assert.IsTrue(m.CompareAndSet(0, 2));
            Assert.IsTrue(m == 2);

            var rs = m + 3;// 2+3
            Assert.AreEqual(5, rs);

            Assert.AreEqual(4, m--);

            Assert.AreEqual(2, m - 2);

            Assert.AreEqual(6, m + 4);

            Assert.AreEqual(6, m.GetAndAdd(4));
            Assert.AreEqual(10, m);
           
        }
Exemplo n.º 6
0
 public void can_get_or_add()
 {
     _num.SetValue(10);
     _num.GetAndAdd(5).Should().Be(10);
     _num.GetValue().Should().Be(15);
 }
 /// <summary>
 ///  Returns the next hash code.
 /// </summary>
 /// <returns></returns>
 private static int NextHashCode()
 {
     return(nextHashCode.GetAndAdd(HASH_INCREMENT));
 }