56 lines
1.1 KiB
Plaintext
56 lines
1.1 KiB
Plaintext
Shared resources should not be used for locking as it increases the chance of deadlocks. Any other thread could acquire (or attempt to acquire) the same lock for another unrelated purpose.
|
|
|
|
|
|
Instead, a dedicated ``++object++`` instance should be used for each shared resource, to avoid deadlocks or lock contention.
|
|
|
|
|
|
The following objects are considered as shared resources:
|
|
|
|
* ``++Me++``
|
|
* a ``++Type++`` object
|
|
* a string literal
|
|
* a string instance
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,vbnet]
|
|
----
|
|
Public Sub MyLockingMethod()
|
|
SyncLock Me 'Noncompliant
|
|
' ...
|
|
End SyncLock
|
|
End Sub
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
[source,vbnet]
|
|
----
|
|
Class MyClass
|
|
Dim lockObj As New Object()
|
|
|
|
Public Sub MyLockingMethod()
|
|
SyncLock lockObj
|
|
' ...
|
|
End SyncLock
|
|
End Sub
|
|
End Class
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|