Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I need to synchronize std::condition_variable/condition_variable_any::notify_one
    primarykey
    data
    text
    <p>Do I need to synchronize <code>std::condition_variable/condition_variable_any::notify_one</code>?</p> <p>As far as I can see, if lost of notifications is acceptable - it is OK to call <code>notify_one</code> not protected (by mutex for example).</p> <p>For instance, I saw following usage patterns (sorry, don't remember where):</p> <pre><code>{ { lock_guard&lt;mutex&gt; l(m); // do work } c.notify_one(); } </code></pre> <hr> <p>But, I inspected libstdc++ sources, and I see:</p> <p><a href="https://github.com/ooPo/gcc-4.7.0-PS3/blob/5d9addb08be92a040b8a8675c9e5a231ce37d705/libstdc%2B%2B-v3/src/c%2B%2B11/condition_variable.cc">condition_variable::notify_one</a></p> <pre><code>void condition_variable::notify_one() noexcept { int __e = __gthread_cond_signal(&amp;_M_cond); // XXX not in spec // EINVAL if (__e) __throw_system_error(__e); } </code></pre> <p>and <a href="https://github.com/ooPo/gcc-4.7.0-PS3/blob/5d9addb08be92a040b8a8675c9e5a231ce37d705/libstdc%2B%2B-v3/include/std/condition_variable">condition_variable_any::notify_one</a>:</p> <pre><code>void condition_variable_any::notify_one() noexcept { lock_guard&lt;mutex&gt; __lock(_M_mutex); _M_cond.notify_one(); } </code></pre> <p>And here is layout of condition_variable_any:</p> <pre><code>class condition_variable_any { condition_variable _M_cond; mutex _M_mutex; // data end </code></pre> <p>I.e. it is just thin wrapper around condition_variable+mutex.</p> <p>So, questions:</p> <ol> <li>Is it thread-safe to not protect <code>notify_one</code> by mutex for either <code>condition_variable_any</code> or <code>condition_variable</code>?</li> <li>Why implementation of condition_variable_any uses additional mutex?</li> <li>Why implementation of <code>condition_variable_any::notify_one</code> and <code>condition_variable::notify_one</code> differs? Maybe <code>condition_variable::notify_one</code> requires manual protection but <code>condition_variable_any::notify_one</code> doesn't? Is it libstdc++ bug?</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload