Files
codeql/csharp/ql/src/Concurrency/UnsynchronizedStaticAccess.qhelp
Bradley Grainger d10597f69d Delete thin space from documentation.
Update the MSDN link to avoid an unnecessary redirection and use the correct anchor.
2023-08-28 11:02:38 -07:00

41 lines
1.3 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
For performance reasons, most collection classes in the standard library are not thread-safe, instead requiring
the user to guarantee they are used from at most one thread at a time by external locking or data structure
invariants.
</p>
<p>
For example, the behavior of <code>Dictionary</code> when a write happens concurrently with another write or a read is
undefined, and frequently leads to data corruption and can lead to issues as serious as livelock.
</p>
</overview>
<recommendation>
<p>
If a static data member such as a <code>Dictionary</code> is likely to be accessed from multiple threads, ensure
that either it is of a concurrency-safe collection type, or that all reads and writes are guarded by a suitable lock or monitor.
</p>
</recommendation>
<example>
<p>
The following code uses a static dictionary to store properties, but provides unsynchronized access to that
dictionary. This means that multiple threads can access the dictionary, potentially leading to a race condition.
</p>
<sample src="UnsynchronizedStaticAccess.cs" />
</example>
<references>
<li>MSDN, C# Reference: <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?#thread-safety">Dictionary: Thread safety</a>.</li>
</references>
</qhelp>