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