<?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:08:13</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Thu, 07 May 2026 08:04:13 +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>race_condition_and_data_race</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2014:08:13:race_condition_and_data_race</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;race_condition_and_data_race&quot;&gt;2014-08-13 - race condition and data race&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
today i had a nice conversation with a friend regarding multithreading. we were figuring out what is the relation between a &lt;a href=&quot;https://en.wikipedia.org/wiki/race condtion&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/race condtion&quot;&gt;race condtion&lt;/a&gt; and a &lt;a href=&quot;https://en.wikipedia.org/wiki/race condition#C.2B.2B&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/race condition#C.2B.2B&quot;&gt;data race&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
first question was: is it possible to have race condition without having a data race? the answer is yes. example proving it:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// thread 1:&lt;/span&gt;
cout &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// thread 2:&lt;/span&gt;
cout &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
program is a classic race condition, as the output is either “12” or “21”. using &lt;em&gt;cout&lt;/em&gt; from different threads is not a data race (though content of both writes may obviously interleave).
&lt;/p&gt;

&lt;p&gt;
so the second question is: is a data race concept a subset of a race condition concept? or another way around: is it possible to have a data race without having a race condition? again the answer is yes! proof:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// global variable:&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// thread 1:&lt;/span&gt;
x &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// thread 2:&lt;/span&gt;
x &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// main:&lt;/span&gt;
joinThread1&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
joinThread2&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
cout &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
there is a clear data race on “x”, but there is no race condition, as the output will always be “1”. of course it is a bit tricky here – program with data race has an undefined behavior, so we cannot be really certain of anything. in practice however, on modern desktop, this particular case works just fine.
&lt;/p&gt;

&lt;p&gt;
a conclusion is that data race and race conditions are independent concepts. you can have either one, (at least theoretically) without having an other.
&lt;/p&gt;

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