2024-11-18 09:48:20 +01:00
|
|
|
include::../description-dotnet.adoc[]
|
2024-11-15 16:21:18 +01:00
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
public class Example
|
|
|
|
{
|
|
|
|
private static ReaderWriterLock rwLock = new();
|
|
|
|
|
|
|
|
public void AcquireWriterLock() =>
|
2024-11-18 09:48:20 +01:00
|
|
|
rwLock.AcquireWriterLock(2000); // Noncompliant, as the lock release is on the callers responsibility
|
2024-11-15 16:21:18 +01:00
|
|
|
|
2024-11-18 09:48:20 +01:00
|
|
|
public void DoSomething()
|
2024-11-15 16:21:18 +01:00
|
|
|
{
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2024-11-18 09:48:20 +01:00
|
|
|
public void ReleaseWriterLock() =>
|
2024-11-15 16:21:18 +01:00
|
|
|
rwLock.ReleaseWriterLock();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
public class Example
|
|
|
|
{
|
|
|
|
private static ReaderWriterLock rwLock = new();
|
|
|
|
|
|
|
|
public void DoSomething()
|
|
|
|
{
|
|
|
|
rwLock.AcquireWriterLock(2000); // Compliant, locks are released in the same method
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// ...
|
|
|
|
}
|
2024-11-18 09:48:20 +01:00
|
|
|
finally
|
2024-11-15 16:21:18 +01:00
|
|
|
{
|
|
|
|
rwLock.ReleaseWriterLock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2024-11-18 09:48:20 +01:00
|
|
|
include::../resources-dotnet.adoc[]
|
2024-11-15 16:21:18 +01:00
|
|
|
|
|
|
|
include::../rspecator.adoc[]
|