Skip to content

Commit

Permalink
explain example a little more
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcmyers committed Nov 26, 2024
1 parent d4be2bf commit 9311cb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lectures/concurrency/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ <h2>When is synchronization needed?</h2>
do not propagate immediately to main memory or to the caches of other processors.
For example, consider
two threads executing the following code in parallel. Say the values of <code>x</code> and <code>y</code>
are initially 0.
are initially 0. The idea is for Thread 2 to wait until Thread 1 finishes some work. It
signals that it is done by setting the variable <code>x</code> to 1:
</p>
<table class=sidebysidecode><tr><td>
Thread 1:
Expand All @@ -576,11 +577,12 @@ <h2>When is synchronization needed?</h2>
Thread 2 could observe the update to <code>x</code> <strong>before</strong> the update to <code>y</code>.
The fact that Thread 1 assigned to <code>y</code> before
it assigned 1 to <code>x</code> does not matter! This behavior can and does happen frequently
with modern hardware. When it happens is hardware-dependent, so that code that seems to work
correctly on one processor (say, an Intel x86 processor) may fail to work on a machine using
an ARM processor. This bizarre state of affairs shows that programming with
concurrency can be highly counterintuitive, and that one must never rely on naive assumptions
about the order of events.
with modern hardware. When it happens is hardware-dependent, so that code that
seems to work correctly on one processor (say, an Intel x86 processor) may fail
to work on a machine using an ARM processor, which makes weaker guarantees.
This bizarre state of affairs shows that programming with concurrency can be
highly counterintuitive, and that it is easy to make bad assumptions about the
order of events.
</p>
<p>
The most reliable way
Expand Down
1 change: 1 addition & 0 deletions lectures/synchronization/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ <h2>Condition variables</h2>
<p>
Given that synchronization is needed for threads to see each other's effects, is there
a way to successfully implement the earlier example that broke due to weak memory consistency?
Recall that the idea was to have one thread wait until another one finished some updates:
</p>

<table class="sidebysidecode"><tr><td>
Expand Down

0 comments on commit 9311cb2

Please sign in to comment.