Exemplo n.º 1
0
            /// <summary>
            /// 多线程并发弹出节点
            /// </summary>
            /// <returns></returns>
            public poolNode Dequeue()
            {
                poolNode head;

                do
                {
                    if ((head = this.head).QueueNextNode == null)
                    {
                        return(null);
                    }
                    if (head.TryUse() != 0)
                    {
                        Thread.Yield();
                        if ((head = this.head).QueueNextNode == null)
                        {
                            return(null);
                        }
                        if (head.TryUse() != 0)
                        {
                            Thread.Sleep(0);
                            continue;
                        }
                    }
                    if (head == this.head)
                    {
                        poolNode node = head.QueueNextNode;
                        if (Interlocked.CompareExchange(ref this.head, node, head) == head)
                        {
                            head.TryPush2();
                            return(node);
                        }
                    }
                    head.tryPush();
                }while (true);
            }
Exemplo n.º 2
0
            /// <summary>
            /// 多线程并发清除节点数据
            /// </summary>
            public void Clear()
            {
                poolNode head;

                do
                {
                    if ((head = this.head) == this.end)
                    {
                        return;
                    }
                    if (head.TryUse() != 0)
                    {
                        Thread.Yield();
                        if ((head = this.head) == this.end)
                        {
                            return;
                        }
                        if (head.TryUse() != 0)
                        {
                            Thread.Sleep(0);
                            continue;
                        }
                    }
                    if (head == this.head)
                    {
                        poolNode end = this.end;
                        if (Interlocked.CompareExchange(ref this.head, end, head) == head)
                        {
                            poolNode node = head.QueueNextNode;
                            head.TryPush2();
                            while (node != end)
                            {
                                head = node.QueueNextNode;
                                node.tryPush();
                                if (head == end)
                                {
                                    return;
                                }
                                node = head.QueueNextNode;
                                head.tryPush();
                            }
                            return;
                        }
                    }
                    head.tryPush();
                }while (true);
            }