Object pool

This commit is contained in:
wrenge 2024-11-13 13:21:41 +03:00
parent 5c0ba9dba1
commit b2a217d4a3
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
namespace DotRecast.Core.Buffers
{
// This implementation is thread unsafe
public class RcObjectPool<T> where T : class
{
private readonly Queue<T> _items = new Queue<T>();
private readonly Func<T> _createFunc;
public RcObjectPool(Func<T> createFunc)
{
_createFunc = createFunc;
}
public T Get()
{
if (_items.TryDequeue(out var result))
return result;
return _createFunc();
}
public void Return(T obj)
{
_items.Enqueue(obj);
}
}
}