Skip to content

Commit fbb9ee5

Browse files
Add back ConcurrentDictionary
1 parent 4feb4a0 commit fbb9ee5

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

api/AltV.Net.Async/AsyncEntityPool.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static async Task<TEntity> GetOrCreateAsync<TEntity>(this IEntityPool<TEn
4343

4444
public abstract class AsyncEntityPool<TEntity> : IEntityPool<TEntity> where TEntity : IEntity
4545
{
46-
private readonly Dictionary<IntPtr, TEntity> entities = new Dictionary<IntPtr, TEntity>();
46+
private readonly ConcurrentDictionary<IntPtr, TEntity> entities = new ConcurrentDictionary<IntPtr, TEntity>();
4747

4848
private readonly IEntityFactory<TEntity> entityFactory;
4949

@@ -82,8 +82,10 @@ public void Create(IntPtr entityPointer, out TEntity entity)
8282
//TODO: what should happen on failure
8383
public void Add(TEntity entity)
8484
{
85-
entities[entity.NativePointer] = entity;
86-
OnAdd(entity);
85+
if (entities.TryAdd(entity.NativePointer, entity))
86+
{
87+
OnAdd(entity);
88+
}
8789
}
8890

8991
public bool Remove(TEntity entity)
@@ -94,7 +96,7 @@ public bool Remove(TEntity entity)
9496
//TODO: what should happen on failure
9597
public bool Remove(IntPtr entityPointer)
9698
{
97-
if (!entities.Remove(entityPointer, out var entity) || !entity.Exists) return false;
99+
if (!entities.TryRemove(entityPointer, out var entity) || !entity.Exists) return false;
98100
lock (entity)
99101
{
100102
BaseObjectPool<TEntity>.SetEntityNoLongerExists(entity);

0 commit comments

Comments
 (0)