<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2080: Specify when once_flag becomes invalid</title>
<meta property="og:title" content="Issue 2080: Specify when once_flag becomes invalid">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2080.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++14">C++14</a> status.</em></p>
<h3 id="2080"><a href="lwg-defects.html#2080">2080</a>. Specify when <code>once_flag</code> becomes invalid</h3>
<p><b>Section:</b> 32.6.7 <a href="https://wg21.link/thread.once">[thread.once]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Nicolai Josuttis <b>Opened:</b> 2011-08-30 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
In function <code>call_once</code> 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a>
paragraph 4 and 5 specify for <code>call_once()</code>:
</p>

<blockquote>
<p>
<i>Throws</i>: <code>system_error</code> when an exception is required (32.2.2 <a href="https://wg21.link/thread.req.exception">[thread.req.exception]</a>), 
or any exception thrown by <code>func</code>.
<p/>
<i>Error conditions</i>:
</p>
<ul>
<li><code>invalid_argument</code> &mdash; if the <code>once_flag</code> object is no longer valid.</li>
</ul>
</blockquote>

<p>
However, nowhere in 32.6.7 <a href="https://wg21.link/thread.once">[thread.once]</a> is specified, when a once-flag becomes invalid.
<p/>
As far as I know this happens if the flag is used for different functions. So we either have to have 
to insert a sentence&#47;paragraph in
</p>
<blockquote><p>
30.4.4.2 Function call_once [thread.once.callonce]
</p></blockquote>
<p>
or
</p>
<blockquote><p>
30.4.4 Call once [thread.once]
</p></blockquote>
<p>
explaining when a <code>once_flag</code> becomes invalidated or we should state as error condition something like:
</p>

<ul>
<li><code>invalid_argument</code> &mdash; if the <code>func</code> used in combination with the <code>once_flag</code> is different 
from a previously passed <code>func</code> for the same <code>once_flag</code>
</li>
</ul>

<p>
Anthony Williams:
</p>
<blockquote>
<p>
A <code>once_flag</code> is invalidated if you destroy it (e.g. it is an automatic object, or heap 
allocated and deleted, etc.)
<p/>
If the library can detect that this is the case then it will throw this exception. If it cannot 
detect such a case then it will never be thrown.
</p>
</blockquote>

<p>
Jonathan Wakely:
</p>
<blockquote>
<p>
I have also wondered how that error can happen in C++, where the type
system will reject a non-callable type being passed to <code>call_once()</code> and
should prevent a <code>once_flag</code> being used after its destructor runs.
<p/>
If a <code>once_flag</code> is used after its destructor runs then it is indeed
undefined behaviour, so implementations are already free to throw any
exception (or set fire to a printer) without the standard saying so.
<p/>
My assumption was that it's an artefact of basing the API on pthreads,
which says:
</p>
<blockquote>
<p>
The <code>pthread_once()</code> function may fail if:
<p/>
<code>[EINVAL]</code>  If either <code>once_control</code> or <code>init_routine</code> is invalid.
</p>
</blockquote>
</blockquote>

<p>
Pete Becker:
</p>
<blockquote><p>
Yes, probably. We had to clean up several UNIXisms that were in the original design.
</p></blockquote>

<p><i>[2012, Kona]</i></p>

<p>
Remove error conditions, move to Review.
</p>

<p><i>[2012, Portland: move to Tentatively Ready]</i></p>

<p>
Concurrency move to Ready, pending LWG review.
</p>

<p>
LWG did not have time to perform the final review in Portland, so moving to
<i>tentatively</i> ready to reflect the Concurrency belief that the issue is
ready, but could use a final inspection from library wordsmiths.
</p>

<p><i>[2013-04-20 Bristol]</i></p>




<p id="res-2080"><b>Proposed resolution:</b></p>

<p>This wording is relative to N3337.</p>

<ol>
<li><p>Change 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a> as indicated:</p>
<blockquote><pre>
template&lt;class Callable, class ...Args&gt;
void call_once(once_flag&amp; flag, Callable&amp;&amp; func, Args&amp;&amp;... args);
</pre><blockquote>
<p>
[&hellip;]
<p/>
-4- <i>Throws</i>: <code>system_error</code> when an exception is required (30.2.2), or any exception thrown by <code>func</code>.
<p/>
<del>-5- Error conditions:</del>
</p>
<ul>
<li><del><code>invalid_argument</code> &mdash; if the <code>once_flag</code> object is no longer valid.</del></li>
</ul>
</blockquote></blockquote>
</li>
</ol>





</body>
</html>
