-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSynchSetUnsynchGet.qhelp
More file actions
42 lines (35 loc) · 1.93 KB
/
SynchSetUnsynchGet.qhelp
File metadata and controls
42 lines (35 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Data which is accessed concurrently from multiple threads is vulnerable to race conditions
and other errors. <code>lock</code> statements are often needed to make concurrent code correct and ensure
that the data is consistent. However <code>lock</code> statements must be used consistently on
all methods which are potentially called concurrently.</p>
<p>When there is a <code>lock</code> statement on a property setter, it implies that the property could be
assigned to concurrently, so the property could also be read concurrently.
Therefore a <code>lock</code> statement is necessary on the property getter. Even simple getters
require a <code>lock</code> statement to ensure that there is a memory barrier when reading a field.
</p>
</overview>
<recommendation>
<p>Examine the logic of the program to check whether the property could be read concurrently.
Add a <code>lock</code> statement in the property getter if necessary.</p>
<p>Alternatively, remove the <code>lock</code> statement from the property setter if it is
no longer needed.</p>
</recommendation>
<example>
<p>This example shows a property setter which uses a <code>lock</code> statement, but there is no
corresponding <code>lock</code> statement in the getter. This means that <code>count</code> is not
synchronized with <code>GenerateDiagnostics()</code>, and there is a read barrier missing from the
property getter, which could cause bugs on some CPU architectures.</p>
<sample src="SynchSetUnsynchGet.cs" />
<p>The solution is to add a <code>lock</code> statement to the property getter, as follows:</p>
<sample src="SynchSetUnsynchGetFix.cs" />
</example>
<references>
<li>MSDN, C# Reference: <a href="http://msdn.microsoft.com/en-gb/library/c5kehkcz.aspx">lock Statement</a>.</li>
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/Memory_barrier">Memory barrier</a>.</li>
</references>
</qhelp>