Skip to content

Commit efda837

Browse files
committed
Fixed concurrency issue
1 parent 453a240 commit efda837

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/DotNext.Threading/Runtime/Caching/RandomAccessCache.Dictionary.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ internal struct Bucket
160160
internal readonly AsyncExclusiveLock Lock;
161161
private bool newPairAdded;
162162
private volatile KeyValuePair? first;
163-
private int count;
163+
private volatile int count;
164164

165165
public Bucket(AsyncExclusiveLock bucketLock) => Lock = bucketLock;
166166

@@ -204,8 +204,8 @@ internal void Add(KeyValuePair pair)
204204
{
205205
pair.NextInBucket = current = tmp;
206206
} while (!ReferenceEquals(tmp = Interlocked.CompareExchange(ref first, pair, current), current));
207-
208-
count++;
207+
208+
Interlocked.Increment(ref count);
209209
}
210210

211211
internal void MarkAsReadyToAdd() => newPairAdded = false;
@@ -221,7 +221,7 @@ private void TryRemove(KeyValuePair? previous, KeyValuePair current)
221221
// on every access (read/write), so it will be eventually deleted
222222
ref var location = ref previous is null ? ref first : ref previous.NextInBucket;
223223
if (ReferenceEquals(Interlocked.CompareExchange(ref location, current.NextInBucket, current), current))
224-
count--;
224+
Interlocked.Decrement(ref count);
225225
}
226226

227227
internal KeyValuePair? TryRemove(IEqualityComparer<TKey>? keyComparer, TKey key, int hashCode)

0 commit comments

Comments
 (0)