Skip to content

Commit

Permalink
Actually I already made this point! Merging...
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcmyers committed Nov 22, 2024
1 parent ce1fada commit d4be2bf
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lectures/concurrency/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ <h3>Volatile variables</h3>
though they have their own mutex: each access to such a variable (read or write) causes
synchronization using that mutex. This mechanism is occasionally useful, but it's not enough
in many situations. For example, declaring the variable <code>balance</code> to be volatile
in the earlier bank example would not help at all; all of the wrong interleavings would still be possible.
in the earlier bank example would not help at all.
Each access will be separately synchronized, so all the bad interleavings remain possible.
</p>
<p>
Some programmers have a bad habit of trying to solve concurrency problems
Expand All @@ -492,25 +493,10 @@ <h3>Volatile variables</h3>
with them can be accomplished with other synchronization mechanisms. And
because they cause automatic synchronization, they are expensive, both because
synchronization has an inherent overhead and because it can remove desirable parallelism.
The time that <code>volatile</code> is useful is when
The <code>volatile</code> declaration is useful is when
all you want is indeed to acquire a mutex around all accesses to a variable;
in this case, <code>volatile</code> is the easiest and cheapest way to do it.
</p>
<p>
We might think that we could fix the <code>Account</code> example from earlier by
declaring <code>balance</code> to be volatile:
</p>

<pre>
<s>private volatile int balance;</s>
</pre>

<p>
But this declaration <em>will not help at all</em>. There are still two
separate accesses to the variable (a read and a write) from each of the methods of
<code>Account</code>. Each access will be separately synchronized, so all
interleavings remain possible.
</p>

<h3>Atomic abstractions</h3>
<p>
Expand Down

0 comments on commit d4be2bf

Please sign in to comment.