<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 891: std::thread, std::call_once issue</title>
<meta property="og:title" content="Issue 891: std::thread, std::call_once issue">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue891.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++11">C++11</a> status.</em></p>
<h3 id="891"><a href="lwg-defects.html#891">891</a>. <code>std::thread</code>, <code>std::call_once</code> issue</h3>
<p><b>Section:</b> 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a>, 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Peter Dimov <b>Opened:</b> 2008-09-15 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#thread.thread.constr">active issues</a> in [thread.thread.constr].</p>
<p><b>View all other</b> <a href="lwg-index.html#thread.thread.constr">issues</a> in [thread.thread.constr].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I notice that the vararg overloads of <code>std::thread</code> and <code>std::call_once</code>
(N2723 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a> and 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a>) are no longer specified in terms of
<code>std::bind</code>; instead, some of the <code>std::bind</code> wording has been inlined into
the specification.
</p>
<p>
There are two problems with this.
</p>
<p>
First, the specification (and implementation) in terms of <code>std::bind</code> allows, for example:
</p>

<blockquote><pre>
std::thread th( f, 1, std::bind( g ) );
</pre></blockquote>

<p>
which executes <code>f( 1, g() )</code> in a thread. This can be useful. The
"inlined" formulation changes it to execute <code>f( 1, bind(g) )</code> in a thread.
</p>
<p>
Second, assuming that we don't want the above, the specification has copied the wording
</p>

<blockquote><p>
<code>INVOKE(func, w1, w2, ..., wN)</code> (20.6.2) shall be a valid
expression for some values <code>w1, w2, ..., wN</code>
</p></blockquote>

<p>
but this is not needed since we know that our argument list is args; it should simply be
</p>

<blockquote><p>
<code>INVOKE(func, args...)</code> (20.6.2) shall be a valid expression
</p></blockquote>

<p><i>[
Summit:
]</i></p>


<blockquote><p>
Move to open.
</p></blockquote>

<p><i>[
Post Summit Anthony provided proposed wording.
]</i></p>


<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Leave Open. Await decision for thread variadic constructor.
</p></blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
See proposed wording for <a href="lwg-defects.html#929" title="Thread constructor (Status: C++11)">929</a><sup><a href="https://cplusplus.github.io/LWG/issue929" title="Latest snapshot">(i)</a></sup> for this, for the formulation
on how to solve this.  <a href="lwg-defects.html#929" title="Thread constructor (Status: C++11)">929</a><sup><a href="https://cplusplus.github.io/LWG/issue929" title="Latest snapshot">(i)</a></sup> modifies the thread constructor to
have "pass by value" behavior with pass by reference efficiency through the use
of the <code>decay</code> trait.  This same formula would be useful for <code>call_once</code>.
</p></blockquote>

<p><i>[
2010-02-11 Anthony updates wording.
]</i></p>


<p><i>[
2010-02-12 Moved to Tentatively Ready after 5 postive votes on c++std-lib.
]</i></p>



<p id="res-891"><b>Proposed resolution:</b></p>
<p>
Modify 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a> p1-p2 with the following:
</p>

<blockquote><pre>
template&lt;class Callable, class ...Args&gt;
  void call_once(once_flag&amp; flag, Callable<ins>&amp;&amp;</ins> func, Args&amp;&amp;... args);
</pre><blockquote>

<p><ins>
Given a function as follows:
</ins></p>

<blockquote><pre><ins>
template&lt;typename T&gt; typename decay&lt;T&gt;::type decay_copy(T&amp;&amp; v)
   { return std::forward&lt;T&gt;(v); }
</ins></pre></blockquote>

<p>
1 <i>Requires:</i> <del>The template parameters</del> <code>Callable</code> and each
<code>Ti</code> in <code>Args</code> shall <del>be <code>CopyConstructible</code> if an
lvalue and otherwise</del> <ins>satisfy the</ins> <code>MoveConstructible</code> 
<ins>requirements</ins>.
<code><i>INVOKE</i>(<ins>decay_copy(std::forward&lt;Callable&gt;(</ins>func<ins>)</ins>,
<del>w1, w2, ..., wN</del>
<ins>decay_copy(std::forward&lt;Args&gt;(args))...</ins>)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>) 
shall be a valid expression<del> for some values <code>w1, w2, ..., wN</code>, where 
<code>N == sizeof...(Args)</code></del>.
</p>

<p>
2 <i>Effects:</i> Calls to <code>call_once</code> on the same <code>once_flag</code>
object are serialized. If there has been a prior effective call to
<code>call_once</code> on the same <code>once_flag</code> object, the call to
<code>call_once</code> returns without invoking <code>func</code>. If there has been no
prior effective call to <code>call_once</code> on the same <code>once_flag</code>
object, <del>the argument <code>func</code> (or a copy thereof) is called as if by
invoking <code>func(args)</code></del>
<ins><code><i>INVOKE</i>(decay_copy(std::forward&lt;Callable&gt;(func)),
decay_copy(std::forward&lt;Args&gt;(args))...)</code> is executed</ins>. The call
to <code>call_once</code> is effective if and only if <del><code>func(args)</code></del>
<ins><code><i>INVOKE</i>(decay_copy(std::forward&lt;Callable&gt;(func)),
decay_copy(std::forward&lt;Args&gt;(args))...)</code></ins> returns without
throwing an exception. If an exception is thrown it is propagated to the caller.
</p>

</blockquote>

</blockquote>







</body>
</html>
