예제 #1
0
 public void Push(SingleLinkNode <T> newNode)
 {
     newNode.Item = default(T);
     do
     {
         newNode.Next = head.Next;
     } while (!SyncMethods.CAS <SingleLinkNode <T> >(ref head.Next, newNode.Next, newNode));
     return;
 }
예제 #2
0
 public bool Pop(out SingleLinkNode <T> node)
 {
     do
     {
         node = head.Next;
         if (node == null)
         {
             return(false);
         }
     } while (!SyncMethods.CAS <SingleLinkNode <T> >(ref head.Next, node, node.Next));
     return(true);
 }
예제 #3
0
        public bool Dequeue(out T item)
        {
            item = default(T);
            SingleLinkNode <T> oldHead = null;

            bool haveAdvancedHead = false;

            while (!haveAdvancedHead)
            {
                oldHead = head;
                SingleLinkNode <T> oldTail     = tail;
                SingleLinkNode <T> oldHeadNext = oldHead.Next;

                if (oldHead == head)
                {
                    if (oldHead == oldTail)
                    {
                        if (oldHeadNext == null)
                        {
                            return(false);
                        }
                        SyncMethods.CAS <SingleLinkNode <T> >(ref tail, oldTail, oldHeadNext);
                    }
                    else
                    {
                        item             = oldHeadNext.Item;
                        haveAdvancedHead = SyncMethods.CAS <SingleLinkNode <T> >(ref head, oldHead, oldHeadNext);
                        if (haveAdvancedHead)
                        {
                            trash.Push(oldHead);
                        }
                    }
                }
            }
            return(true);
        }