<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx</link><description>The 5.0 release of C# introduced the await keyword which makes it extremely easy to use Task&amp;lt;T&amp;gt; in a non-blocking fashion.&amp;#160; This allows developers to replace either blocking calls to Task.Wait() or complicated combinations of ContinueWith and</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10394905</link><pubDate>Mon, 18 Feb 2013 16:16:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10394905</guid><dc:creator>JaredPar MSFT</dc:creator><description>&lt;p&gt;@Leonid it is indeed atomic but that&amp;#39;s not the code that is executed here. &amp;nbsp;If you look at the IL it&amp;#39;s essentially&lt;/p&gt;
&lt;p&gt;ldfield m_sum&lt;/p&gt;
&lt;p&gt;await y &lt;/p&gt;
&lt;p&gt;add&lt;/p&gt;
&lt;p&gt;stfield m_sum&lt;/p&gt;
&lt;p&gt;The &amp;quot;add&amp;quot; IL instruction operates on the 2 values which are pushed on the IL stack. &amp;nbsp;The m_sum value is pushed atomically but it is also cached on the stack. &amp;nbsp;While waiting for `await y` to finish, it&amp;#39;s very possible that another operation will start running which modifies m_sum. &amp;nbsp;This will modify the field only and not the value which is cached on the stack. &amp;nbsp;Hence when it resumes it will add the previous value of m_sum to &amp;#39;await y&amp;#39; and erase the intermediate m_sum value&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10394905" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10394792</link><pubDate>Mon, 18 Feb 2013 10:34:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10394792</guid><dc:creator>Leonid Tcarev</dc:creator><description>&lt;p&gt;@Jared, why?&lt;/p&gt;
&lt;p&gt;add m_sum, eax is atomic.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10394792" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10394050</link><pubDate>Fri, 15 Feb 2013 16:51:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10394050</guid><dc:creator>JaredPar MSFT</dc:creator><description>&lt;p&gt;@Leonid the write of the int32 value is atomic but the read of the original value can occur on a separate turn from the write. &amp;nbsp;This is what makes it unsafe &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10394050" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10393936</link><pubDate>Fri, 15 Feb 2013 10:56:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10393936</guid><dc:creator>Leonid Tcarev</dc:creator><description>&lt;p&gt;&amp;gt;The first add could get preempted after it reads m_sum, and the second thread could run and update &amp;gt;m_sum, and then the first thread would trample the second thread&amp;#39;s update. &lt;/p&gt;
&lt;p&gt;Adding of something to int32 bit variable (not property) should be atomic at x86&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10393936" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10393471</link><pubDate>Wed, 13 Feb 2013 18:48:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10393471</guid><dc:creator>Ian Obermiller</dc:creator><description>&lt;p&gt;Oh, right. That is what makes it even more interesting (and deadly) I suppose. Thanks!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10393471" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10393075</link><pubDate>Tue, 12 Feb 2013 19:25:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10393075</guid><dc:creator>JaredPar MSFT</dc:creator><description>&lt;p&gt;@Ian&lt;/p&gt;
&lt;p&gt;This is actually a bug which happens equally as well for single threaded and multi-threaded code. &amp;nbsp;The repro code I showed all executes on a single thread. &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10393075" width="1" height="1"&gt;</description></item><item><title>re: Don’t mix await and compound assignment</title><link>http://blogs.msdn.com/b/jaredpar/archive/2013/02/11/don-t-mix-await-and-compound-assignment.aspx#10393073</link><pubDate>Tue, 12 Feb 2013 19:23:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10393073</guid><dc:creator>Ian Obermiller</dc:creator><description>&lt;p&gt;Since you are referencing multi-threading bugs, isn&amp;#39;t the example with a temporary value still unsafe? The first add could get preempted after it reads m_sum, and the second thread could run and update m_sum, and then the first thread would trample the second thread&amp;#39;s update. So the correct code would need some kind of synchronization, like Interlocked.Add.&lt;/p&gt;
&lt;p&gt;The main point though is a good one, and problems like this are becoming much easier with await.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10393073" width="1" height="1"&gt;</description></item></channel></rss>