<?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:2015:12:13</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 09:38:16 +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>condition_variable_and_lock</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2015:12:13:condition_variable_and_lock</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;condition_variable_and_lock&quot;&gt;2015-12-13 - condition variable and lock&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
some time ago i was asked to make a code review of a multi-threaded code part. i found a code block like this:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;condition_variable&lt;/span&gt; cv&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;mutex&lt;/span&gt;              m&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// ...a lot of code here...&lt;/span&gt;
std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;unique_lock&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;mutex&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt; lock&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;
cv.&lt;span class=&quot;me1&quot;&gt;notify_one&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
and commented it as a “no-no” – why to notify, while still having mutex locked?! it&amp;#039;s obvious other thread will do something like this:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;unique_lock&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;mutex&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt; lock&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;
cv.&lt;span class=&quot;me1&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;lock, someConditionCheck&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// ... now process&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
so it means we&amp;#039;re waking up a thread, just so it can block on mutex, then we unlock and wake it up again, so that it can process. sounds like a waste, doesn&amp;#039;t it?
&lt;/p&gt;

&lt;p&gt;
well – it appears it does not work that way. in fact &lt;a href=&quot;https://computing.llnl.gov/tutorials/pthreads/man/pthread_cond_signal.txt&quot; class=&quot;urlextern&quot; title=&quot;https://computing.llnl.gov/tutorials/pthreads/man/pthread_cond_signal.txt&quot; rel=&quot;ugc nofollow&quot;&gt;POSIX advises signaling under a lock&lt;/a&gt;. dunno what are implementation details here, but doing this:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;m.&lt;span class=&quot;me1&quot;&gt;lock&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
cv.&lt;span class=&quot;me1&quot;&gt;notify_one&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;
m.&lt;span class=&quot;me1&quot;&gt;unlock&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
performs better than this:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;m.&lt;span class=&quot;me1&quot;&gt;lock&lt;/span&gt;&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;co1&quot;&gt;// ...&lt;/span&gt;
m.&lt;span class=&quot;me1&quot;&gt;unlock&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;
cv.&lt;span class=&quot;me1&quot;&gt;notify_one&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
which was quite surprising for me.
&lt;/p&gt;

&lt;p&gt;
in fact the gain for linux system was ~3-10% (depending on the particular run and the machine test were run on), while at my friend&amp;#039;s windows platform difference was as high as 30% (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2015:12:13:condition_variable_and_lock.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2015:12:13:condition_variable_and_lock.cpp (1 KB)&quot;&gt;reference implementation&lt;/a&gt;)!
&lt;/p&gt;

&lt;p&gt;
even though i still do not know why it is so, the good news is that naturally written &lt;a href=&quot;https://en.wikipedia.org/wiki/RAII&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/RAII&quot;&gt;RAII&lt;/a&gt; code:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;lock_guard&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;mutex&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt; lock&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;
&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
cv.&lt;span class=&quot;me1&quot;&gt;notify_one&lt;/span&gt;&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;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
works as expected and is superior, when it comes to performance.
&lt;/p&gt;

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