<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1046: Provide simple facility to start asynchronous operations</title>
<meta property="og:title" content="Issue 1046: Provide simple facility to start asynchronous operations">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1046.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#Resolved">Resolved</a> status.</em></p>
<h3 id="1046"><a href="lwg-defects.html#1046">1046</a>. Provide simple facility to start asynchronous operations</h3>
<p><b>Section:</b> 32.10 <a href="https://wg21.link/futures">[futures]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-03-12 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures">issues</a> in [futures].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses UK 329 [CD1]</b></p>

<p>
<code>future</code>, <code>promise</code> and <code>packaged_task</code> provide a
framework for creating future values, but a simple function to tie all
three components together is missing. Note that we only need a <em>simple</em>
facility for C++0x. Advanced thread pools are to be left for TR2.
</p>

<p>
Simple Proposal:
</p>

<p>
Provide a simple function along the lines of: 
</p>
<blockquote><pre>
template&lt; typename F, typename ... Args &gt;
  requires Callable&lt; F, Args... &gt;
    future&lt; Callable::result_type &gt; async( F&amp;&amp; f, Args &amp;&amp; ... ); 
</pre></blockquote>

<p>
Semantics are similar to creating a <code>thread</code> object with a <code>packaged_task</code>
invoking <code>f</code> with <code>forward&lt;Args&gt;(args...)</code>
but details are left unspecified to allow different scheduling and thread
spawning implementations. 
</p>
<p>
It is unspecified whether a task submitted to async is run on its own thread
or a thread previously used for another async task. If a call to <code>async</code>
succeeds, it shall be safe to wait for it from any thread. 
</p>
<p>
The state of <code>thread_local</code> variables shall be preserved during <code>async</code> calls. 
</p>
<p>
No two incomplete async tasks shall see the same value of
<code>this_thread::get_id()</code>. 
</p>
<p>
[<i>Note:</i> this effectively forces new tasks to be run on a new thread, or a
fixed-size pool with no queue. If the 
library is unable to spawn a new thread or there are no free worker threads
then the <code>async</code> call should fail. <i>--end note</i>] 
</p>

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


<blockquote>
<p>
The concurrency subgroup has revisited this issue and decided that it
could be considered a defect according to the Kona compromise. A task
group was formed lead by Lawrence Crowl and Bjarne Stroustrup to write a
paper for Frankfort proposing a simple asynchronous launch facility
returning a <code>future</code>. It was agreed that the callable must be run on a
separate thread from the caller, but not necessarily a brand-new thread.
The proposal might or might not allow for an implementation that uses
fixed-size or unlimited thread pools.
</p>
<p>
Bjarne in c++std-lib-23121: I think that what we agreed was that to
avoid deadlock <code>async()</code> would almost certainly be specified to launch in
a different thread from the thread that executed <code>async()</code>, but I don't
think it was a specific design constraint.
</p>
</blockquote>

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


<blockquote><p>
Proposed resolution: see
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2996.htm">N2996</a>
(Herb's and Lawrence's paper on Async). Move state to <del>NAD editorial</del><ins>Resolved</ins>.
</p></blockquote>



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





</body>
</html>
