<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">	<channel>		<title>You Can Do Any Kind of Atomic Read-Modify-Write Operation Comments</title>		<language>en-us</language>		<link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation</link>		<description>Comments from You Can Do Any Kind of Atomic Read-Modify-Write Operation</description><item>
<title>Charles</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1043600363</link><description>Hi Jeff,  When I use memory_order_acq_rel on an RMW, my understanding is that it prevents memory operations from moving down below the operation (release) and from moving up above the operation (acquire). Does that mean it acts as a full barrier (including #StoreLoad) ? If so, what is the difference with using memory_order_seq_cst on an RMW call ?  </description><pubDate>Mon, 15 May 2017 16:34:10 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1043600363</guid></item><item>
<title>Francesco</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1038466734</link><description>Thank Jeff,   sorry for the strange questions but in my daily-job language (Java) there are no atomic read/write with relaxed order (until now) only read-acquire/write-release ones, hence the java memory model accustom the devs to use read-acquire/write-release for every thread communication... About the reordering logic: the CAS loop I&amp;#039;ve written but using relaxed order doesn&amp;#039;t suffer for reordering of any kind? This is the part the scares me the most...  </description><pubDate>Tue, 7 Feb 2017 11:27:51 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1038466734</guid></item><item>
<title>Francesco</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1038463235</link><description>Hi Jeff! Thanks for the blog, is pure gold!!!! I&amp;#039;m a bit confused by the failure and success parameters in:  bool compare_exchange_strong( T&amp;amp; expected, T desired,                               std::memory_order success,                                std::memory_order failure ) volatile;  In a loop that do:  local_x = x.loadAcquire  do{     ...check if local_x is valid to compute next_x and exit if not...     next_x = ...cool computation that use local_x...      }while(x.cas(&amp;amp;local_x,next_x,Release,Relaxed);  The acquire and release order (at start and at the end) here is not necessary to perform a correct CAS loop, but to ensure that: 1) all the instructions inside the (failed iterations of the) loop can&amp;#039;t be pushed outside of it (it is a single-thread compiler/HW constraint) 2) correct acquire and release semantics for any other shared memory access inside the loop  It is true? Or i&amp;#039;m using the acquire-release semantic when is not necessary? I generally tends to use write-release operation when I want to transfer data between threads, but if the data is x itself, the relaxed order could be enough to maintain the correct single-threaded logic (no hoisting etc etc) and thread-safe operation atomicity?  Sorry for the long question :(    </description><pubDate>Tue, 7 Feb 2017 09:54:48 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1038463235</guid></item><item>
<title>Zab</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1031212908</link><description>Hi Jeff  Can you please help me understand the guarantees behind Read-Write atomic operation?  Here is my simple scenario: 1. One singleton writer process is allowed to increment a shared 32-bits unsigned integer in a non-atomic fashion (i = i + 1) 2. In the other side, N-reader processes are reading this shared unsigned integer using an atomic read (__atomic_add_fetch).  Is it Ok? I mean does the CPU guarantees that the readers will be able to read valid values ? Or can something strange happens in between, while writing to this 32-bits integer and reading from it ?  I&amp;#039;m completely lost with the guarantees here. Maybe you can blog about it.   Thank you.    </description><pubDate>Mon, 3 Oct 2016 09:23:55 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1031212908</guid></item><item>
<title>AR4</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1008170933</link><description>Jeff, I love your blog, and its been in my RSS lists for years now. Unfortunately it looks like my reader is starting to choke on the size of the feed. Any chance you could cut it down to just the last year or so?  Looking forward to more posts! </description><pubDate>Tue, 29 Dec 2015 23:26:26 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment1008170933</guid></item><item>
<title>@jcdickinson</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment998386269</link><description>Shouldn&amp;#039;t fetch_mulitply re-load oldValue in the while loop body? </description><pubDate>Thu, 8 Oct 2015 10:43:27 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment998386269</guid></item><item>
<title>EugeneZ</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment996501032</link><description>Jeff, thanks for the read! I composed some comments to the draft (N4296) of a proposed C++17 Standard. Are they correct?  &amp;gt;&amp;gt;&amp;gt; &lt;a href=&quot;https://parallella.org/forums/viewtopic.php?f=13&amp;amp;t=3212&quot; target=&quot;_blank&quot;&gt;https://parallella.org/forums/viewtopic.php?f=13&amp;...&lt;/a&gt; </description><pubDate>Thu, 24 Sep 2015 13:23:53 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment996501032</guid></item><item>
<title>Sean</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment989706943</link><description>Hi Jeff,  What do you use for the graphics/pictures on your blog? It&amp;#039;s presented very well. </description><pubDate>Mon, 10 Aug 2015 10:29:04 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment989706943</guid></item><item>
<title>Gautam Goel</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment979894413</link><description>Hi Jeff,  Love your blog. Quick question: what Octopress theme are you using? The blog looks great! Thanks. </description><pubDate>Mon, 22 Jun 2015 05:25:15 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment979894413</guid></item><item>
<title>Eugene</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment978852552</link><description>what will the correct memory_order for simple bounded stack implementation?  size_t stack_buffer[256]; td::atomic_uint_fast8_t stack_pointer(0);  void push(const size_t value) {     // the out of bounds write assumed to not ever happen     stack_buffer[stack_pointer.fetch_add(1, std::memory_order_release)] = value; } bool pop(size_t&amp;amp; value) { uint_fast8_t oldStatus(stack_pointer.load(std::memory_order_relaxed)); size_t id; do { if(oldStatus == 0) return; value = stack_buffer[oldStatus-1]; } while (!stack_pointer.compare_exchange_weak(oldStatus, oldStatus-1, std::memory_order_acquire,                                 std::memory_order_relaxed)); }  i&amp;#039;ve noticed that using default memory_order for compare_exchange_weak and fetch_add can really hurt performance on mobile platforms. but i&amp;#039;m not particular sure if my use is correct. thanx </description><pubDate>Wed, 17 Jun 2015 11:43:10 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment978852552</guid></item><item>
<title>Jeff Preshing</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment970479760</link><description>std::atomic&amp;lt;&amp;gt;::fetch_add() is an RMW. So is InterlockedIncrement() in Win32. I don&amp;#039;t know what API exposes atomic_increment(), but based on the name, it&amp;#039;s likely to be an RMW too. </description><pubDate>Mon, 11 May 2015 11:34:06 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment970479760</guid></item><item>
<title>Hari</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment970469645</link><description>I have a question. If you are updating stats from multiple threads, say: struct {     uint64_t count; }; Would atomic_increment(&amp;amp;s.count) suffice or would you have to do RMW? Seems to me you have to use RMW. </description><pubDate>Mon, 11 May 2015 11:12:31 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment970469645</guid></item><item>
<title>Jeff Preshing</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961644399</link><description>Yes. There is actually a limit on the number of RMW operations that can happen on the same address per second, no matter how many threads you throw at it. On my quad-core MacBook, I once measured it at around 20 ns per RMW operation. With four threads running on four cores, that means each thread could only get in there once every 80 ns, on average. On my dual-processor PowerMac G5, the rate was much lower and I&amp;#039;m pretty sure individual threads could be made to starve indefinitely. So, I think you can make it happen in synthetic tests, but even in that case, at least there&amp;#039;s still progress being made across the whole system. </description><pubDate>Fri, 3 Apr 2015 09:15:08 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961644399</guid></item><item>
<title>Nathan Reed</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961609456</link><description>Do you ever run into starvation issues with CAS loops, if several threads are hammering on the same shared variable with CAS loops?  Suppose thread A has to do a bunch of extra work in the &amp;quot;modify&amp;quot; step for some reason, such that one of threads B, C, ... has almost always gotten in and updated the variable by the time A gets to the CAS call.  Then A might have to retry many, many times before it succeeds.  Maybe this situation is contrived enough that it&amp;#039;s not actually a problem in practice. :) </description><pubDate>Fri, 3 Apr 2015 05:29:09 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961609456</guid></item><item>
<title>Jeff Preshing</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961586613</link><description>You&amp;#039;re right. Thanks for the precision! I&amp;#039;ve since changed that sentence to &amp;quot;If it fails, it loads the current value of shared back into expected.&amp;quot; It doesn&amp;#039;t make any difference when you pass a local variable to expected (as is done here), but it&amp;#039;s obviously an important difference in the cases described in that Stack Overflow link, where a shared variable is passed in. </description><pubDate>Fri, 3 Apr 2015 03:15:17 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961586613</guid></item><item>
<title>bames53</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961483563</link><description>This quote is not quite correct: &amp;quot;Simultaneously, it always loads the current value of shared back into expected&amp;quot;  compare_exchange_weak/strong should not write to `expected` if the exchange is successful. `expected` may be a shared variable, and a successful write may be used to indicate that the thread is finished with its work and that other threads can now safely read and write that shared variable. In such a case, if compare_exchange were to write to expected again it could create a race and stomp on writes by other threads.  The C++ spec states that expected is written to only if the exchange fails [n4140 &amp;sect; 29.6.5 / 21]. Originally, implementations did not correctly guarantee this, but MSVC, gcc, and clang have since been fixed.  See my stackoverflow answer for more details: &lt;a href=&quot;http://stackoverflow.com/a/21946549/365496&quot; target=&quot;_blank&quot;&gt;http://stackoverflow.com/a/21946549/365496&lt;/a&gt; </description><pubDate>Thu, 2 Apr 2015 17:22:27 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961483563</guid></item><item>
<title>Jeff Preshing</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961473935</link><description>Well, C\+\+11 atomic operations are always &lt;a href=&quot;http:\/\/preshing.com\/20130618\/atomic-vs-non-atomic-operations&quot; target=&quot;_blank&quot;&gt;atomic&lt;/a&gt;. They&#039;re just not always lock-free. The library was designed that way to achieve maximum portability while maintaining correctness. It only guarantees lock-freedom if std::atomic&amp;lt;T&amp;gt;::is_lock_free() returns a non-zero value for that type.        When C\+\+11 atomic operations are not lock-free, then yea, they will compile to code that uses something equivalent to a mutex. You can test it by wrapping a very large struct in an atomic&amp;lt;&amp;gt;. In VS2012/x86, the compiler implements a simple per-object spinlock, for example.  </description><pubDate>Thu, 2 Apr 2015 16:30:00 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961473935</guid></item><item>
<title>zeuxcg</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961464848</link><description>This is interesting. Is there any reason why std::atomic&amp;lt;T&amp;gt; can&amp;#039;t fail to compile if lock-free code can&amp;#039;t be generated? Silently making atomic operations non-atomic seems... bad. Or will atomic&amp;lt;T&amp;gt; with unsupported T compile to a code that uses a global mutex or something of the sort? </description><pubDate>Thu, 2 Apr 2015 15:39:30 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961464848</guid></item><item>
<title>Jeff Preshing</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961448606</link><description>compare_exchange_weak always reloads oldValue on failure. oldValue is passed by reference. </description><pubDate>Thu, 2 Apr 2015 14:05:24 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961448606</guid></item><item>
<title>Matt Fisher</title><link>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961440042</link><description>I especially like the concurrent fetch_multiply thread graphic.  There appears to be a potential infinite loop in your first while(!compare_exchange_weak()); code, since it never reloads oldValue in the case of failure.  If the other thread sets a new value, it will never succeed. The other compare_exchange_weak instances use do-while, so they don&amp;#039;t suffer from the same danger. </description><pubDate>Thu, 2 Apr 2015 13:16:10 +0000</pubDate><guid>http://127.0.0.1:4000/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation#IDComment961440042</guid></item>	</channel></rss>