<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://www.baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2014:05:30</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 11:51:52 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://www.baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://www.baszerr.eu/</link>
        </image>
        <item>
            <title>multi-threading_is_not_that_obvious</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2014:05:30:multi-threading_is_not_that_obvious</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;multi-threading_is_not_that_obvious&quot;&gt;2014-05-30 - multi-threading is not that obvious&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
writing multi-threaded program is not easy. using atomics correctly is one step harder. when it comes to relaxed atomics it&amp;#039;s a sort of a black-belt level. ;) but to the point…
&lt;/p&gt;

&lt;p&gt;
recently i&amp;#039;ve read an interesting article: &lt;a href=&quot;http://preshing.com/20131125/acquire-and-release-fences-dont-work-the-way-youd-expect&quot; class=&quot;urlextern&quot; title=&quot;http://preshing.com/20131125/acquire-and-release-fences-dont-work-the-way-youd-expect&quot; rel=&quot;ugc nofollow&quot;&gt;Acquire and Release Fences Don&amp;#039;t Work the Way You&amp;#039;d Expect&lt;/a&gt;. this is positive meaning – they actually do a bit better, to ensure your code can be easily made valid, at a cost of little naming-confusion. the funny thing is that author point out that &lt;a href=&quot;https://en.wikipedia.org/wiki/Herb Sutter&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/Herb Sutter&quot;&gt;Herb Sutter&lt;/a&gt; made a mistake himself, explaining how this mechanism works, during his &lt;a href=&quot;http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2&quot; rel=&quot;ugc nofollow&quot;&gt;atomic&amp;lt;&amp;gt; weapons&lt;/a&gt; talk (which is great, btw – watch it, if you haven&amp;#039;t done so yet).
&lt;/p&gt;

&lt;p&gt;
this reminded me of my @ conversation with &lt;a href=&quot;http://hboehm.info&quot; class=&quot;urlextern&quot; title=&quot;http://hboehm.info&quot; rel=&quot;ugc nofollow&quot;&gt;Hans Boehm&lt;/a&gt; regarding his presentation on Going Native 2012: &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Threads-and-Shared-Variables-in-C-11&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Threads-and-Shared-Variables-in-C-11&quot; rel=&quot;ugc nofollow&quot;&gt;threads and shared variables in C++11&lt;/a&gt; (another great position to watch!). around 31:00 a following pseudo-code example appeared:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy3&quot;&gt;!&lt;/span&gt;initd&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  lock_guard&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;mutex&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt; _&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;m&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  x&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  initd&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kw2&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
read x&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
this is a simplified version of &lt;a href=&quot;https://en.wikipedia.org/wiki/DCLP&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/DCLP&quot;&gt;DCLP&lt;/a&gt;. in this code, there is a race on “initd”, as it is not atomic and is read and set from multiple threads, at the same time. Hans fixed this by making “initd” atomic, around 46:06. this code however has still a race, this time on “x”. if you look closely, multiple threads may enter “if” statement, all but one will block on locking a mutex, one will proceed, set “x” and unlock. then it uses (reads) x. however threads that were sleeping previously, will now awake and re-set “x”, while others are already reading it. Hans confirmed, this example is oversimplified – what is missing is a second check on “initd”, before setting x. although this conversation took place about 20 months after the initial presentation, he told me i was the first one to notice that.
&lt;/p&gt;

&lt;p&gt;
Hans is the chair of the concurrency work group. Herb is the chair of the whole committee. if these guys make mistakes on their presentations, broadcasted world-wide, this means that the subject is highly non-trivial indeed. it is hard, often non-predictive (it&amp;#039;s hard to make such an issue repeatable, say in automated tests) and sometimes hardware-dependent (keep in mind x86* has pretty strong memory model, especially when reading from memory). there are also not that many tools that can help you debug such a conditions. concurrency is now The Way to exploit the hardware, but there is still a long road ahead of us, before it can truly reach the masses.
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:08:55 +0000</pubDate>
        </item>
    </channel>
</rss>
