I am trying to understand Interlocked
in C# in thread synchronization.
public int MethodUsedByMultipleThreads()
{
var id = CreateNextId();
return id;
}
private long CreateNextId()
{
long id = 0;
Interlocked.Exchange(ref id , this._nextId);
Interlocked.Increment(ref this._nextId);
return id;
}
Is the line
Interlocked.Exchange(ref id , this._nextId);
redundant if I directly use
Interlocked.Increment(ref this._nextId);
return _nextId;
Will it serve the same purpose?