Read int value in thread safe way

39 views Asked by At

I have
int counter
And have increment it in multithread by
Interlocked.Increment(ref counter);
Is thread safe reading it in other thread (while others still incrementing it) like this:
int localSaved = counter;
or
int localSaved = Interlocked.CompareExchange(ref counter, 0, 0);?
Not really know how 'Interlocked.CompareExchange' work but found it in google.

0

There are 0 answers