File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments