/// <summary> /// 将某一项队列压入栈 /// </summary> /// <param name="item">操作的数据</param> public bool Enqueue(T item) { DoubleTrackNode <T> current = this.end; DoubleTrackNode <T> newitem = new DoubleTrackNode <T>(item); while (true) { var temp = Interlocked.CompareExchange <DoubleTrackNode <T> >(ref this.end, newitem, current); if (temp == current) { /*此时队列没有数据,head==end为头*/ if (this.head == null) { this.head = newitem; } if (current != null) { current.NextNode = newitem; newitem.PreNode = current; current = newitem; } Interlocked.Increment(ref this.count); break; } else { current = this.end; } } return(true); }
/// <summary> /// 从队列头中返回某一项 /// </summary> /// <param name="item">操作的数据</param> /// <returns></returns> public bool Dequeue(out T item) { item = default(T); if (this.head == null) { return(false); } DoubleTrackNode <T> current = this.head; DoubleTrackNode <T> newTop = current == null ? null : current.NextNode; while (true) { var temp = Interlocked.CompareExchange <DoubleTrackNode <T> >(ref this.head, newTop, current); if (temp == current) { if (current != null) { item = current.Current; } Interlocked.Decrement(ref this.count); break; } else { current = this.head; if (current != null) { newTop = current.NextNode; } } } return(true); }
/// <summary> /// 执行与释放或重置非托管资源相关的应用程序定义的任务。 /// </summary> public void Dispose() { this.head = null; this.end = null; }