NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Net/NetQueue.cs

74 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//////////////////////////////////////////////////////////////////////////
//
// 文件Assets/Code/Scripts/Framework/Net/Base/Queue.cs
// 作者Xoen Xie
// 时间2023/07/18
// 描述:一个简单的无锁队列,同一时刻只允许一个线程读,一个线程写
// 说明:
//
//////////////////////////////////////////////////////////////////////////
using System;
using UnityEngine;
namespace Framework
{
public sealed class NetQueue<T>
{
class _Node
{
public T data;
public _Node next;
}
private _Node m_Head;
private _Node m_Tail;
private ulong m_nPushCount;
private ulong m_nPopCount;
public NetQueue()
{
m_Head = new _Node();
m_Head.next = null;
m_Tail = m_Head;
}
public void Push(T data)
{
_Node newNode = new _Node();
newNode.data = data;
newNode.next = null;
m_Tail.next = newNode;
m_Tail = newNode;
++m_nPushCount;
}
public bool Pop(out T data)
{
if (m_Head.next != null)
{
++m_nPopCount;
m_Head = m_Head.next;
data = m_Head.data;
return true;
}
data = default(T);
return false;
}
public ulong Count
{
get
{
return m_nPushCount - m_nPopCount;
}
}
}
}