<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 929: Thread constructor</title>
<meta property="og:title" content="Issue 929: Thread constructor">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue929.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="929"><a href="lwg-defects.html#929">929</a>. Thread constructor</h3>
<p><b>Section:</b> 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Anthony Williams <b>Opened:</b> 2008-10-23 <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><b>Addresses UK 323</b></p>

<p>
The <code>thread</code> constructor for starting a new thread with a function and
arguments is overly constrained by the signature requiring rvalue
references for <code>func</code> and <code>args</code> and the <code>CopyConstructible</code> requirements
for the elements of <code>args</code>. The use of an rvalue reference for the
function restricts the potential use of a plain function name, since
the type of the bound parameter will be deduced to be a function
reference and decay to pointer-to-function will not happen. This
therefore complicates the implementation in order to handle a simple
case. Furthermore, the use of rvalue references for args prevents the
array to pointer decay. Since arrays are not <code>CopyConstructible</code> or even
<code>MoveConstructible</code>, this essentially prevents the passing of arrays as
parameters. In particular it prevents the passing of string literals.
Consequently a simple case such as
</p>

<blockquote><pre>
void f(const char*);
std::thread t(f,"hello");
</pre></blockquote>

<p>
is ill-formed since the type of the string literal is <code>const char[6]</code>.
</p>

<p>
By changing the signature to take all parameters by value we can
eliminate the <code>CopyConstructible</code> requirement and permit the use of
arrays, as the parameter passing semantics will cause the necessary
array-to-pointer decay. They will also cause the function name to
decay to a pointer to function and allow the implementation to handle
functions and function objects identically.
</p>

<p>
The new signature of the <code>thread</code> constructor for a function and
arguments is thus:
</p>

<blockquote><pre>
template&lt;typename F,typename... Args&gt;
thread(F,Args... args);
</pre></blockquote>

<p>
Since the parameter pack <code>Args</code> can be empty, the single-parameter
constructor that takes just a function by value is now redundant.
</p>

<p><i>[
Howard adds:
]</i></p>


<blockquote>
<p>
I agree with everything Anthony says in this issue.  However I believe we
can optimize in such a way as to get the pass-by-value behavior with the
pass-by-rvalue-ref performance.  The performance difference is that the latter
removes a <code>move</code> when passing in an lvalue.
</p>

<p>
This circumstance is very analogous to <code>make_pair</code> (22.3 <a href="https://wg21.link/pairs">[pairs]</a>)
where we started with passing by const reference, changed to pass by value to
get pointer decay, and then changed to pass by rvalue reference, but modified with
<code>decay&lt;T&gt;</code> to retain the pass-by-value behavior.  If we were to
apply the same solution here it would look like:
</p>

<blockquote><pre>
<del>template &lt;class F&gt; explicit thread(F f);</del>
template &lt;class F, class ...Args&gt; thread(F&amp;&amp; f, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-4- <i>Requires:</i> <code>F</code> and each <code>Ti</code> in <code>Args</code> shall be <del><code>CopyConstructible</code>
if an lvalue and otherwise</del> <code>MoveConstructible</code>.
<code><i>INVOKE</i>(f, w1, w2, ..., wN)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>) shall be a valid expression for
some values <code>w1, w2, ... , wN,</code> where <code>N == sizeof...(Args)</code>.
</p>
<p>
-5- <i>Effects:</i> Constructs an object of type <code>thread</code>
<del>and executes <code><i>INVOKE</i>(f, t1, t2, ..., tN)</code> in a new
thread of execution, where <code>t1, t2, ..., tN</code> are the values in <code>args...</code></del>.
<ins>Constructs
the following objects in memory which is accessible to a new thread of execution
as if:</ins>
</p>
<blockquote><pre>
<ins>typename decay&lt;F&gt;::type g(std::forward&lt;F&gt;(f));</ins>
<ins>tuple&lt;typename decay&lt;Args&gt;::type...&gt; w(std::forward&lt;Args&gt;(args)...);</ins>
</pre></blockquote>
<p>
<ins>The new thread of
execution executes <code><i>INVOKE</i>(g, wi...)</code> where the <code>wi...</code> refers
to the elements stored in the <code>tuple w</code>.</ins>
Any return value from <code>g</code> is ignored.
<del>If <code>f</code> terminates with an uncaught exception, <code>std::terminate()</code> shall be called.</del>
<ins>If the evaluation of <code><i>INVOKE</i>(g,  wi...)</code> terminates
with an uncaught exception, <code>std::terminate()</code> shall be called [<i>Note:</i>
<code>std::terminate()</code> could be called before entering <code>g</code>. -- <i>end note</i>]. Any
exception thrown before the evaluation of <code><i>INVOKE</i></code> has started shall be
catchable in the calling thread.</ins>
</p>
</blockquote>
</blockquote>

<p>
Text referring to when <code>terminate()</code> is called was contributed by Ganesh.
</p>

</blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
We agree with the proposed resolution,
but would like the final sentence to be reworded
since "catchable" is not a term of art (and is used nowhere else).
</p></blockquote>

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


<blockquote>
<p>
This is linked to
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2901.pdf">N2901</a>.
</p>
<p>
Howard to open a separate issue to remove (<a href="lwg-closed.html#1176" title="Make thread constructor non-variadic (Status: NAD)">1176</a><sup><a href="https://cplusplus.github.io/LWG/issue1176" title="Latest snapshot">(i)</a></sup>).
</p>
<p>
In Frankfurt there is no consensus for removing the variadic constructor.
</p>
</blockquote>

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


<blockquote><p>
We want to move forward with this issue.  If we later take it out via <a href="lwg-closed.html#1176" title="Make thread constructor non-variadic (Status: NAD)">1176</a><sup><a href="https://cplusplus.github.io/LWG/issue1176" title="Latest snapshot">(i)</a></sup>
then that's ok too.  Needs small group to improve wording.
</p></blockquote>

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


<blockquote>
<p>
Stefanus provided revised wording.  Moved to Review  Here is the original wording:
</p>
<blockquote>
<p>
Modify the class definition of <code>std::thread</code> in 32.4.3 <a href="https://wg21.link/thread.thread.class">[thread.thread.class]</a> to remove the
following signature:
</p>

<blockquote><pre>
<del>template&lt;class F&gt; explicit thread(F f);</del>
template&lt;class F, class ... Args&gt; <ins>explicit</ins> thread(F&amp;&amp; f, Args&amp;&amp; ... args);
</pre></blockquote>

<p>
Modify 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a> to replace the constructors prior to paragraph 4 with
the single constructor as above. Replace paragraph 4 - 6 with the
following:
</p>

<blockquote>
<p>
-4- <i>Requires:</i> <code>F</code> and each <code>Ti</code> in <code>Args</code> shall be <del><code>CopyConstructible</code>
if an lvalue and otherwise</del> <code>MoveConstructible</code>.
<code><i>INVOKE</i>(f, w1, w2, ..., wN)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>) shall be a valid expression for
some values <code>w1, w2, ... , wN,</code> where <code>N == sizeof...(Args)</code>.
</p>
<p>
-5- <i>Effects:</i> Constructs an object of type <code>thread</code>
<del>and executes <code><i>INVOKE</i>(f, t1, t2, ..., tN)</code> in a new
thread of execution, where <code>t1, t2, ..., tN</code> are the values in <code>args...</code></del>.
<ins>Constructs
the following objects:</ins>
</p>
<blockquote><pre>
<ins>typename decay&lt;F&gt;::type g(std::forward&lt;F&gt;(f));</ins>
<ins>tuple&lt;typename decay&lt;Args&gt;::type...&gt; w(std::forward&lt;Args&gt;(args)...);</ins>
</pre></blockquote>
<p>
<ins>and executes <code><i>INVOKE</i>(g, wi...)</code> in a new thread of execution.
These objects shall be destroyed when the new thread of execution completes.</ins>
Any return value from <code>g</code> is ignored.
<del>If <code>f</code> terminates with an uncaught exception, <code>std::terminate()</code> shall be called.</del>
<ins>If the evaluation of <code><i>INVOKE</i>(g,  wi...)</code> terminates
with an uncaught exception, <code>std::terminate()</code> shall be called [<i>Note:</i>
<code>std::terminate()</code> could be called before entering <code>g</code>. -- <i>end note</i>]. Any
exception thrown before the evaluation of <code><i>INVOKE</i></code> has started shall be
catchable in the calling thread.</ins>
</p>
<p>
-6- <i>Synchronization:</i> The invocation of the constructor <i>happens before</i> the
invocation of <del><code>f</code></del> <ins><code>g</code></ins>.
</p>
</blockquote>

</blockquote>
</blockquote>

<p><i>[
2010-01-19 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>



<p id="res-929"><b>Proposed resolution:</b></p>
<p>
Modify the class definition of <code>std::thread</code> in 32.4.3 <a href="https://wg21.link/thread.thread.class">[thread.thread.class]</a> to remove the
following signature:
</p>

<blockquote><pre>
<del>template&lt;class F&gt; explicit thread(F f);</del>
template&lt;class F, class ... Args&gt; <ins>explicit</ins> thread(F&amp;&amp; f, Args&amp;&amp; ... args);
</pre></blockquote>

<p>
Modify 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a> to replace the constructors prior to paragraph 4 with
the single constructor as above. Replace paragraph 4 - 6 with the
following:
</p>

<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>
-4- <i>Requires:</i> <code>F</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>.
<del><code><i>INVOKE</i>(f, w1, w2, ..., wN)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>)
shall be a valid expression for some values <code>w1, w2, ... , wN,</code> where
<code>N == sizeof...(Args)</code>.</del>
<ins><code><i>INVOKE</i>(decay_copy(std::forward&lt;F&gt;(f)), decay_copy(std::forward&lt;Args&gt;(args))...)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>) shall be a valid expression.</ins>
</p>

<p>
-5- <i>Effects:</i> Constructs an object of type <code>thread</code> <del>and executes
<code>INVOKE(f, t1, t2, ..., tN)</code> in a new thread of execution, where
<code>t1, t2, ..., tN</code> are the values in <code>args...</code>. 
Any return
value from <code>f</code> is ignored. If <code>f</code> terminates with an
uncaught exception, <code>std::terminate()</code> shall be called.</del>
<ins>The new thread of execution executes <code>INVOKE(decay_copy(std::forward&lt;F&gt;(f)),
decay_copy(std::forward&lt;Args&gt;(args))...)</code> with the calls to <code>decay_copy()</code> being evaluated in
the constructing thread. Any return value from this invocation is
ignored. [<i>Note:</i> this implies any exceptions not thrown from the
invocation of the copy of <code>f</code> will be thrown in the constructing thread,
not the new thread. &mdash; <i>end note</i>].
If the invocation of <code><i>INVOKE</i>(decay_copy(std::forward&lt;F&gt;(f)),
decay_copy(std::forward&lt;Args&gt;(args))...)</code> terminates with an uncaught
exception, <code>std::terminate</code> shall be called.
</ins></p>

<p>
-6- <i>Synchronization:</i> The invocation of the constructor <i>happens before</i> the
invocation of <ins>the copy of</ins> <code>f</code>.
</p>
</blockquote>






</body>
</html>
