Simulating lock in async code

async

To lock a critical code in async fragment we should use SemaphoreSlim as in the following example


private static readonly SemaphoreSlim _syncObj = new (1,1);

...

await _syncObj.WaitAsync();

try {
	...
}
finally {
	_syncObj.Release();
}

Post a Comment

Previous Post Next Post