dotnet / dotnet/dotnet-api-docs

Improvements to supplied example

Open
#1,011 0 comments 0 reactions 0 assignees View on GitHub
area-System.Threading dotnet-api/prod Pri2 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

Looks like the example above had a bit of an issue.
It seems to me instead of:
if(0 == Interlocked.Exchange(ref usingResource, 1))
the line should say the following:
if(0 == Interlocked.CompareExchange(ref usingResource, 1, 0))

otherwise what's the point of such "resource sharing" locking?
Also i would place resource unlocking code as part of "finally" block something like that:
...
}
finally
{
Interlocked.Exchange(ref usingResource, 0);
}
}
else
{

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: f9f4cc4a-f153-5ee2-42e6-849e7842d22a
* Version Independent ID: aea8070a-f00d-0beb-e932-8fcb9dbd1f66
* Content: [Interlocked Class (System.Threading)](https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked?view=netframework-4.7.2)
* Content Source: [xml/System.Threading/Interlocked.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Threading/Interlocked.xml)
* Product: **dotnet-api**
* GitHub Login: @rpetrusha
* Microsoft Alias: **ronpet**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.