Understanding threading synchronization
<p>The Fredrik Lundh article titled <a href="http://effbot.org/zone/thread-synchronization.htm" rel="nofollow"><em>Thread Synchronization Mechanisms in Python</em></a> gives the example below to show that multiple threads operating can result in an inaccurate value.</p>

<pre><code>counter = 0

def process_item(item):
    global counter
    ... do something with item ...
    counter += 1
</code></pre>

<p>Then it goes on saying these things are thread-safe:</p>

<pre class="lang-none prettyprint-override"><code>reading or replacing a single instance attribute
reading or replacing a single global variable
fetching an item from a list
modifying a list in place (e.g. adding an item using append)
fetching an item from a dictionary
modifying a dictionary in place (e.g. adding an item, or calling the clear method)
</code></pre>

<p>But isn't the code example just updating a single global variable, and hence would be thread-safe anyway?</p>

<p>What am I missing here?</p>

<p>In Python the code:</p>

<pre><code>counter += 1
</code></pre>

<p>is the same as saying:</p>

<pre><code>counter = counter + 1
</code></pre>

<p>Thus the lookup of the value is separate to the update.</p>

python multithreading